5 A Linker for ca65 Object modules
7 (C) Copyright 1998-2000 Ullrich von Bassewitz
21 4. Output configuration files
24 4.3 Builtin configurations
35 The ld65 linker combines several object modules created by the ca65
36 assembler, producing an executable file. The object modules may be read
37 from a library created by the ar65 archiver (this is somewhat faster and
38 more convenient). The linker was designed to be as flexible as possible.
39 It complements the features that are built into the ca65 macroassembler:
41 * Accept any number of segments to form an executable module.
43 * Resolve arbitrary expressions stored in the object files.
45 * In case of errors, use the meta information stored in the object files
46 to produce helpful error messages. In case of undefined symbols,
47 expression range errors, or symbol type mismatches, ld65 is able to
48 tell you the exact location in the original assembler source, where
49 the symbol was referenced.
51 * Flexible output. The output of ld65 is highly configurable by a
52 config file. More common platforms are supported by builtin
53 configurations that may be activated by naming the target system.
54 The output generation was designed with different output formats in
55 mind, so adding other formats shouldn't be a great problem.
62 The linker is called as follows:
64 ---------------------------------------------------------------------------
65 Usage: ld65 [options] module ...
68 -m name Create a map file
69 -o name Name the default output file
70 -t sys Set the target system
73 -C name Use linker config file
74 -Ln name Create a VICE label file
75 -Lp Mark write protected segments as such (VICE)
76 -S addr Set the default start address
77 -V Print the linker version
80 --help Help (this text)
81 --mapfile name Create a map file
82 --target sys Set the target system
83 --version Print the linker version
84 ---------------------------------------------------------------------------
90 Print the short option summary shown above.
96 This option (which needs an argument that will used as a filename for
97 the generated map file) will cause the linker to generate a map file.
98 The map file does contain a detailed overview over the modules used, the
99 sizes for the different segments, and a table containing exported
105 The -o switch is used to give the name of the default output file.
106 Depending on your output configuration, this name may NOT be used as
107 name for the output file. However, for the builtin configurations, this
108 name is used for the output file name.
113 The argument for the -t switch is the name of the target system. Since
114 this switch will activate a builtin configuration, it may not be used
115 together with the -C option. The following target systems are currently
128 There are a few more targets defined but neither of them is actually
129 supported. See section 4.3 for more information about the builtin
136 Using the -v option, you may enable more output that may help you to
137 locate problems. If an undefined symbol is encountered, -v causes the
138 linker to print a detailed list of the references (that is, source file
139 and line) for this symbol.
144 Must be used in conjunction with -m (generate map file). Normally the
145 map file will not include empty segments and sections, or unreferenced
146 symbols. Using this option, you can force the linker to include all
147 this information into the map file.
152 This gives the name of an output config file to use. See section 4 for
153 more information about config files. -C may not be used together with
159 This option allows you to create a file that contains all global labels
160 and may be loaded into VICE emulator using the pb (playback) command.
161 You may use this to debug your code with VICE. Note: The label feature
162 is very new in VICE and has some bugs. If you have problems, please get
163 the latest VICE version.
173 Using -S you may define the default starting address. If and how this
174 address is used depends on the config file in use. For the builtin
175 configurations, only the "none" system honors an explicit start address,
176 all other builtin config provide their own.
182 This option print the version number of the linker. If you send any
183 suggestions or bugfixes, please include this number.
186 If one of the modules is not found in the current directory, and the
187 module name does not have a path component, the value of the environment
188 variable CC65_LIB is prepended to the name, and the linker tries to open
189 the module with this new name.
196 The linker does several things when combining object modules:
198 First, the command line is parsed from left to right. For each object file
199 encountered (object files are recognized by a magic word in the header, so
200 the linker does not care about the name), imported and exported
201 identifiers are read from the file and inserted in a table. If a library
202 name is given (libraries are also recognized by a magic word, there are no
203 special naming conventions), all modules in the library are checked if an
204 export from this module would satisfy an import from other modules. All
205 modules where this is the case are marked. If duplicate identifiers are
206 found, the linker issues a warning.
208 This procedure (parsing and reading from left to right) does mean, that a
209 library may only satisfy references for object modules (given directly or
210 from a library) named BEFORE that library. With the command line
212 ld65 crt0.o clib.lib test.o
214 the module test.o may not contain references to modules in the library
215 clib.lib. If this is the case, you have to change the order of the modules
218 ld65 crt0.o test.o clib.lib
220 Step two is, to read the configuration file, and assign start addresses
221 for the segments and define any linker symbols (see section 4).
223 After that, the linker is ready to produce an output file. Before doing
224 that, it checks it's data for consistency. That is, it checks for
225 unresolved externals (if the output format is not relocatable) and for
226 symbol type mismatches (for example a zero page symbol is imported by a
227 module as absolute symbol).
229 Step four is, to write the actual target files. In this step, the linker
230 will resolve any expressions contained in the segment data. Circular
231 references are also detected in this step (a symbol may have a circular
232 reference that goes unnoticed if the symbol is not used).
234 Step five is to output a map file with a detailed list of all modules,
235 segments and symbols encountered.
237 And, last step, if you give the -v switch twice, you get a dump of the
238 segment data. However, this may be quite unreadable if you're not a
243 4. Output configuration files
244 -----------------------------
246 Configuration files are used to describe the layout of the output file(s).
247 Two major topics are covered in a config file: The memory layout of the
248 target architecture, and the assignment of segments to memory areas. In
249 addition, several other attributes may be specified.
251 Case is ignored for keywords, that is, section or attribute names, but it
252 is NOT ignored for names and strings.
259 Memory areas are specified in a "MEMORY" section. Lets have a look at an
260 example (this one describes the usable memory layout of the C64):
263 RAM1: start = $0800, size = $9800;
264 ROM1: start = $A000, size = $2000;
265 RAM2: start = $C000, size = $1000;
266 ROM2: start = $E000, size = $2000;
269 As you can see, there are two ram areas and two rom areas. The names
270 (before the colon) are arbitrary names that must start with a letter, with
271 the remaining characters being letters or digits. The names of the memory
272 areas are used when assigning segments. As mentioned above, case is
273 significant for these names.
275 The syntax above is used in all sections of the config file. The name
276 ("ROM1" etc.) is said to be an identifier, the remaining tokens up to the
277 semicolon specify attributes for this identifier. You may use the equal
278 sign to assign values to attributes, and you may use a comma to separate
279 attributes, you may also leave both out. But you MUST use a semicolon to
280 mark the end of the attributes for one identifier. The section above may
281 also have looked like this:
283 # Start of memory section
300 There are of course more attributes for a memory section than just start
301 and size. Start and size are mandatory attributes, that means, each memory
302 area defined MUST have these attributes given (the linker will check
303 that). I will cover other attributes later. As you may have noticed, I've
304 used a comment in the example above. Comments start with a hash mark
305 (`#'), the remainder of the line is ignored if this character is found.
307 Let's assume you have written a program for your trusty old C64, and you
308 would like to run it. For testing purposes, it should run in the RAM area.
309 So we will start to assign segments to memory sections in the SEGMENTS
313 CODE: load = RAM1, type = ro;
314 RODATA: load = RAM1, type = ro;
315 DATA: load = RAM1, type = rw;
316 BSS: load = RAM1, type = bss, define = yes;
319 What we are doing here is telling the linker, that all segments go into
320 the RAM1 memory area in the order specified in the SEGMENTS section. So
321 the linker will first write the CODE segment, then the RODATA segment,
322 then the DATA segment - but it will not write the BSS segment. Why? Enter
323 the segment type: For each segment specified, you may also specify a
324 segment attribute. There are five possible segment attributes:
327 wprot same as ro but will be marked as write protected in
328 the VICE label file if -Lp is given
330 bss means that this is an uninitialized segment
331 empty will not go in any output file
333 So, because we specified that the segment with the name BSS is of type
334 bss, the linker knows that this is uninitialized data, and will not write
335 it to an output file. This is an important point: For the assembler, the
336 BSS segment has no special meaning. You specify, which segments have the
337 bss attribute when linking. This approach is much more flexible than
338 having one fixed bss segment, and is a result of the design decision to
339 supporting an arbitrary segment count.
341 If you specify "type = bss" for a segment, the linker will make sure that
342 this segment does only contain uninitialized data (that is, zeroes), and
343 issue a warning if this is not the case.
345 For a bss type segment to be useful, it must be cleared somehow by your
346 program (this happens usually in the startup code - for example the
347 startup code for cc65 generated programs takes care about that). But how
348 does your code know, where the segment starts, and how big it is? The
349 linker is able to give that information, but you must request it. This is,
350 what we're doing with the "define = yes" attribute in the BSS definitions.
351 For each segment, where this attribute is true, the linker will export
354 __NAME_LOAD__ This is set to the address where the segment is
356 __NAME_RUN__ This is set to the run address of the segment.
357 We will cover run addresses later.
358 __NAME_SIZE__ This is set to the segment size.
360 Replace "NAME" by the name of the segment, in the example above, this
361 would be "BSS". These symbols may be accessed by your code.
363 Now, as we've configured the linker to write the first three segments and
364 create symbols for the last one, there's only one question left: Where
365 does the linker put the data? It would be very convenient to have the data
366 in a file, wouldn't it?
368 We don't have any files specified above, and indeed, this is not needed in
369 a simple configuration like the one above. There is an additional
370 attribute "file" that may be specified for a memory area, that gives a
371 file name to write the area data into. If there is no file name given, the
372 linker will assign the default file name. This is "a.out" or the one given
373 with the -o option on the command line. Since the default behaviour is ok
374 for our purposes, I did not use the attribute in the example above. Let's
375 have a look at it now.
377 The "file" attribute (the keyword may also be written as "FILE" if you
378 like that better) takes a string enclosed in double quotes (`"') that
379 specifies the file, where the data is written. You may specifiy the same
380 file several times, in that case the data for all memory areas having this
381 file name is written into this file, in the order of the memory areas
382 defined in the MEMORY section. Let's specify some file names in the MEMORY
386 RAM1: start = $0800, size = $9800, file = %O;
387 ROM1: start = $A000, size = $2000, file = "rom1.bin";
388 RAM2: start = $C000, size = $1000, file = %O;
389 ROM2: start = $E000, size = $2000, file = "rom2.bin";
392 The %O used here is a way to specify the default behaviour explicitly: %O
393 is replaced by a string (including the quotes) that contains the default
394 output name, that is, "a.out" or the name specified with the -o option on
395 the command line. Into this file, the linker will first write any segments
396 that go into RAM1, and will append then the segments for RAM2, because the
397 memory areas are given in this order. So, for the RAM areas, nothing has
400 We've not used the ROM areas, but we will do that below, so we give the
401 file names here. Segments that go into ROM1 will be written to a file
402 named "rom1.bin", and segments that go into ROM2 will be written to a file
403 named "rom2.bin". The name given on the command line is ignored in both
406 Let us look now at a more complex example. Say, you've successfully tested
407 your new "Super Operating System" (SOS for short) for the C64, and you
408 will now go and replace the ROMs by your own code. When doing that, you
409 face a new problem: If the code runs in RAM, we need not to care about
410 read/write data. But now, if the code is in ROM, we must care about it.
411 Remember the default segments (you may of course specify your own):
414 RODATA read only data
416 BSS uninitialized data, read/write
418 Since the BSS is not initialized, we must not care about it now, but what
419 about DATA? DATA contains initialized data, that is, data that was
420 explicitly assigned a value. And your program will rely on these values on
421 startup. Since there's no other way to remember the contents of the data
422 segment, than storing it into one of the ROMs, we have to put it there.
423 But unfortunately, ROM is not writeable, so we have to copy it into RAM
424 before running the actual code.
426 The linker cannot help you copying the data from ROM into RAM (this must
427 be done by the startup code of your program), but it has some features
428 that will help you in this process.
430 First, you may not only specify a "load" attribute for a segment, but also
431 a "run" attribute. The "load" attribute is mandatory, and, if you don't
432 specify a "run" attribute, the linker assumes that load area and run area
433 are the same. We will use this feature for our data area:
436 CODE: load = ROM1, type = ro;
437 RODATA: load = ROM2, type = ro;
438 DATA: load = ROM2, run = RAM2, type = rw, define = yes;
439 BSS: load = RAM2, type = bss, define = yes;
442 Let's have a closer look at this SEGMENTS section. We specify that the
443 CODE segment goes into ROM1 (the one at $A000). The readonly data goes
444 into ROM2. Read/write data will be loaded into ROM2 but is run in RAM2.
445 That means that all references to labels in the DATA segment are relocated
446 to be in RAM2, but the segment is written to ROM2. All your startup code
447 has to do is, to copy the data from it's location in ROM2 to the final
450 So, how do you know, where the data is located? This is the second point,
451 where you get help from the linker. Remember the "define" attribute? Since
452 we have set this attribute to true, the linker will define three external
453 symbols for the data segment that may be accessed from your code:
455 __DATA_LOAD__ This is set to the address where the segment is
456 loaded, in this case, it is an address in ROM2.
457 __DATA_RUN__ This is set to the run address of the segment, in
458 this case, it is an address in RAM2.
459 __DATA_SIZE__ This is set to the segment size.
461 So, what your startup code must do, is to copy __DATA_SIZE__ bytes from
462 __DATA_LOAD__ to __DATA_RUN__ before any other routines are called. All
463 references to labels in the DATA segment are relocated to RAM2 by the
464 linker, so things will work properly.
466 There are some other attributes not covered above. Before starting the
467 reference section, I will discuss the remaining things here.
469 You may request symbols definitions also for memory areas. This may be
470 useful for things like a software stack, or an i/o area.
473 STACK: start = $C000, size = $1000, define = yes;
476 This will define three external symbols that may be used in your code:
478 __STACK_START__ This is set to the start of the memory
479 area, $C000 in this example.
481 __STACK_SIZE__ The size of the area, here $1000.
484 __STACK_LAST__ This is NOT the same as START+SIZE.
485 Instead, it it defined as the first
486 address that is not used by data. If we
487 don't define any segments for this area,
488 the value will be the same as START.
490 A memory section may also have a type. Valid types are
492 ro for readonly memory
493 and rw for read/write memory.
495 The linker will assure, that no segment marked as read/write or bss is put
496 into a memory area that is marked as readonly.
498 Unused memory in a memory area may be filled. Use the "fill = yes"
499 attribute to request this. The default value to fill unused space is zero.
500 If you don't like this, you may specify a byte value that is used to fill
501 these areas with the "fillval" attribute. This value is also used to fill
502 unfilled areas generated by the assemblers .ALIGN and .RES directives.
504 Segments may be aligned to some memory boundary. Specify "align = num" to
505 request this feature. Num must be a power of two. To align all segments on
509 CODE: load = ROM1, type = ro, align = $100;
510 RODATA: load = ROM2, type = ro, align = $100;
511 DATA: load = ROM2, run = RAM2, type = rw, define = yes,
513 BSS: load = RAM2, type = bss, define = yes, align = $100;
516 If an alignment is requested, the linker will add enough space to the
517 output file, so that the new segment starts at an address that is
518 divideable by the given number without a remainder. All addresses are
519 adjusted accordingly. To fill the unused space, bytes of zero are used,
520 or, if the memory area has a "fillval" attribute, that value. Alignment is
521 always needed, if you have the used the .ALIGN command in the assembler.
522 The alignment of a segment must be equal or greater than the alignment
523 used in the .ALIGN command. The linker will check that, and issue a
524 warning, if the alignment of a segment is lower than the alignment
525 requested in a .ALIGN command of one of the modules making up this
528 For a given segment you may also specify a fixed offset into a memory area or
529 a fixed start address. Use this if you want the code to run at a specific
530 address (a prominent case is the interrupt vector table which must go at
531 address $FFFA). Only one of ALIGN or OFFSET or START may be specified. If the
532 directive creates empty space, it will be filled with zero, of with the value
533 specified with the "fillval" attribute if one is given. The linker will warn
534 you if it is not possible to put the code at the specified offset (this may
535 happen if other segments in this area are too large). Here's an example:
538 VECTORS: load = ROM2, type = ro, start = $FFFA;
541 or (for the segment definitions from above)
544 VECTORS: load = ROM2, type = ro, offset = $1FFA;
547 File names may be empty, data from segments assigned to a memory area with
548 an empty file name is discarded. This is useful, if the a memory area has
549 segments assigned that are empty (for example because they are of type
550 bss). In that case, the linker will create an empty output file. This may
551 be suppressed by assigning an empty file name to that memory area.
553 The symbol %S may be used to access the default start address (that is,
554 $200 or the value given on the command line with the -S option).
563 4.3 Builtin configurations
564 --------------------------
566 Here is a list of the builin configurations for the different target
571 RAM: start = %S, size = $10000, file = %O;
574 CODE: load = RAM, type = rw;
575 RODATA: load = RAM, type = rw;
576 DATA: load = RAM, type = rw;
577 BSS: load = RAM, type = bss, define = yes;
582 HEADER: start = $0000, size = $6, file = %O;
583 RAM: start = $1F00, size = $6100, file = %O;
586 EXEHDR: load = HEADER, type = wprot;
587 CODE: load = RAM, type = wprot, define = yes;
588 RODATA: load = RAM, type = wprot;
589 DATA: load = RAM, type = rw;
590 BSS: load = RAM, type = bss, define = yes;
591 AUTOSTRT: load = RAM, type = wprot;
596 RAM: start = $7FF, size = $c801, file = %O;
599 CODE: load = RAM, type = ro;
600 RODATA: load = RAM, type = ro;
601 DATA: load = RAM, type = rw;
602 BSS: load = RAM, type = bss, define = yes;
607 RAM: start = $1bff, size = $a401, file = %O;
610 CODE: load = RAM, type = ro;
611 RODATA: load = RAM, type = ro;
612 DATA: load = RAM, type = rw;
613 BSS: load = RAM, type = bss, define = yes;
621 RAM: start = $0fff, size = $7001, file = %O;
624 CODE: load = RAM, type = ro;
625 RODATA: load = RAM, type = ro;
626 DATA: load = RAM, type = rw;
627 BSS: load = RAM, type = bss, define = yes;
632 RAM: start = $0001, size = $FFF0, file = %O;
635 CODE: load = RAM, type = ro;
636 RODATA: load = RAM, type = ro;
637 DATA: load = RAM, type = rw;
638 BSS: load = RAM, type = bss, define = yes;
643 RAM: start = $03FF, size = $7BFF, file = %O;
646 CODE: load = RAM, type = ro;
647 RODATA: load = RAM, type = ro;
648 DATA: load = RAM, type = rw;
649 BSS: load = RAM, type = bss, define = yes;
654 RAM: start = $800, size = $8E00, file = %O;
657 CODE: load = RAM, type = ro;
658 RODATA: load = RAM, type = ro;
659 DATA: load = RAM, type = rw;
660 BSS: load = RAM, type = bss, define = yes;
665 HEADER: start = $204, size = 508, file = %O;
666 RAM: start = $400, size = $7C00, file = %O;
669 HEADER: load = HEADER, type = ro;
670 CODE: load = RAM, type = ro;
671 RODATA: load = RAM, type = ro;
672 DATA: load = RAM, type = rw;
673 BSS: load = RAM, type = bss, define = yes;
676 The "start" attribute for the RAM memory area of the CBM systems is two
677 less than the actual start of the basic RAM to account for the two bytes
678 load address that is needed on disk and supplied by the startup code.
685 If you have problems using the linker, if you find any bugs, or if you're
686 doing something interesting with it, I would be glad to hear from you.
687 Feel free to contact me by email (uz@musoftware.de).
694 ld65 (and all cc65 binutils) are (C) Copyright 1998-2000 Ullrich von
695 Bassewitz. For usage of the binaries and/or sources the following
698 This software is provided 'as-is', without any expressed or implied
699 warranty. In no event will the authors be held liable for any damages
700 arising from the use of this software.
702 Permission is granted to anyone to use this software for any purpose,
703 including commercial applications, and to alter it and redistribute it
704 freely, subject to the following restrictions:
706 1. The origin of this software must not be misrepresented; you must not
707 claim that you wrote the original software. If you use this software
708 in a product, an acknowledgment in the product documentation would be
709 appreciated but is not required.
710 2. Altered source versions must be plainly marked as such, and must not
711 be misrepresented as being the original software.
712 3. This notice may not be removed or altered from any source