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.
174 Using -S you may define the default starting address. If and how this
175 address is used depends on the config file in use. For the builtin
176 configurations, only the "none" system honors an explicit start address,
177 all other builtin config provide their own.
183 This option print the version number of the linker. If you send any
184 suggestions or bugfixes, please include this number.
187 If one of the modules is not found in the current directory, and the
188 module name does not have a path component, the value of the environment
189 variable CC65_LIB is prepended to the name, and the linker tries to open
190 the module with this new name.
197 The linker does several things when combining object modules:
199 First, the command line is parsed from left to right. For each object file
200 encountered (object files are recognized by a magic word in the header, so
201 the linker does not care about the name), imported and exported
202 identifiers are read from the file and inserted in a table. If a library
203 name is given (libraries are also recognized by a magic word, there are no
204 special naming conventions), all modules in the library are checked if an
205 export from this module would satisfy an import from other modules. All
206 modules where this is the case are marked. If duplicate identifiers are
207 found, the linker issues a warning.
209 This procedure (parsing and reading from left to right) does mean, that a
210 library may only satisfy references for object modules (given directly or
211 from a library) named BEFORE that library. With the command line
213 ld65 crt0.o clib.lib test.o
215 the module test.o may not contain references to modules in the library
216 clib.lib. If this is the case, you have to change the order of the modules
219 ld65 crt0.o test.o clib.lib
221 Step two is, to read the configuration file, and assign start addresses
222 for the segments and define any linker symbols (see section 4).
224 After that, the linker is ready to produce an output file. Before doing
225 that, it checks it's data for consistency. That is, it checks for
226 unresolved externals (if the output format is not relocatable) and for
227 symbol type mismatches (for example a zero page symbol is imported by a
228 module as absolute symbol).
230 Step four is, to write the actual target files. In this step, the linker
231 will resolve any expressions contained in the segment data. Circular
232 references are also detected in this step (a symbol may have a circular
233 reference that goes unnoticed if the symbol is not used).
235 Step five is to output a map file with a detailed list of all modules,
236 segments and symbols encountered.
238 And, last step, if you give the -v switch twice, you get a dump of the
239 segment data. However, this may be quite unreadable if you're not a
244 4. Output configuration files
245 -----------------------------
247 Configuration files are used to describe the layout of the output file(s).
248 Two major topics are covered in a config file: The memory layout of the
249 target architecture, and the assignment of segments to memory areas. In
250 addition, several other attributes may be specified.
252 Case is ignored for keywords, that is, section or attribute names, but it
253 is NOT ignored for names and strings.
260 Memory areas are specified in a "MEMORY" section. Lets have a look at an
261 example (this one describes the usable memory layout of the C64):
264 RAM1: start = $0800, size = $9800;
265 ROM1: start = $A000, size = $2000;
266 RAM2: start = $C000, size = $1000;
267 ROM2: start = $E000, size = $2000;
270 As you can see, there are two ram areas and two rom areas. The names
271 (before the colon) are arbitrary names that must start with a letter, with
272 the remaining characters being letters or digits. The names of the memory
273 areas are used when assigning segments. As mentioned above, case is
274 significant for these names.
276 The syntax above is used in all sections of the config file. The name
277 ("ROM1" etc.) is said to be an identifier, the remaining tokens up to the
278 semicolon specify attributes for this identifier. You may use the equal
279 sign to assign values to attributes, and you may use a comma to separate
280 attributes, you may also leave both out. But you MUST use a semicolon to
281 mark the end of the attributes for one identifier. The section above may
282 also have looked like this:
284 # Start of memory section
301 There are of course more attributes for a memory section than just start
302 and size. Start and size are mandatory attributes, that means, each memory
303 area defined MUST have these attributes given (the linker will check
304 that). I will cover other attributes later. As you may have noticed, I've
305 used a comment in the example above. Comments start with a hash mark
306 (`#'), the remainder of the line is ignored if this character is found.
308 Let's assume you have written a program for your trusty old C64, and you
309 would like to run it. For testing purposes, it should run in the RAM area.
310 So we will start to assign segments to memory sections in the SEGMENTS
314 CODE: load = RAM1, type = ro;
315 RODATA: load = RAM1, type = ro;
316 DATA: load = RAM1, type = rw;
317 BSS: load = RAM1, type = bss, define = yes;
320 What we are doing here is telling the linker, that all segments go into
321 the RAM1 memory area in the order specified in the SEGMENTS section. So
322 the linker will first write the CODE segment, then the RODATA segment,
323 then the DATA segment - but it will not write the BSS segment. Why? Enter
324 the segment type: For each segment specified, you may also specify a
325 segment attribute. There are five possible segment attributes:
328 wprot same as ro but will be marked as write protected in
329 the VICE label file if -Lp is given
331 bss means that this is an uninitialized segment
332 empty will not go in any output file
334 So, because we specified that the segment with the name BSS is of type
335 bss, the linker knows that this is uninitialized data, and will not write
336 it to an output file. This is an important point: For the assembler, the
337 BSS segment has no special meaning. You specify, which segments have the
338 bss attribute when linking. This approach is much more flexible than
339 having one fixed bss segment, and is a result of the design decision to
340 supporting an arbitrary segment count.
342 If you specify "type = bss" for a segment, the linker will make sure that
343 this segment does only contain uninitialized data (that is, zeroes), and
344 issue a warning if this is not the case.
346 For a bss type segment to be useful, it must be cleared somehow by your
347 program (this happens usually in the startup code - for example the
348 startup code for cc65 generated programs takes care about that). But how
349 does your code know, where the segment starts, and how big it is? The
350 linker is able to give that information, but you must request it. This is,
351 what we're doing with the "define = yes" attribute in the BSS definitions.
352 For each segment, where this attribute is true, the linker will export
355 __NAME_LOAD__ This is set to the address where the segment is
357 __NAME_RUN__ This is set to the run address of the segment.
358 We will cover run addresses later.
359 __NAME_SIZE__ This is set to the segment size.
361 Replace "NAME" by the name of the segment, in the example above, this
362 would be "BSS". These symbols may be accessed by your code.
364 Now, as we've configured the linker to write the first three segments and
365 create symbols for the last one, there's only one question left: Where
366 does the linker put the data? It would be very convenient to have the data
367 in a file, wouldn't it?
369 We don't have any files specified above, and indeed, this is not needed in
370 a simple configuration like the one above. There is an additional
371 attribute "file" that may be specified for a memory area, that gives a
372 file name to write the area data into. If there is no file name given, the
373 linker will assign the default file name. This is "a.out" or the one given
374 with the -o option on the command line. Since the default behaviour is ok
375 for our purposes, I did not use the attribute in the example above. Let's
376 have a look at it now.
378 The "file" attribute (the keyword may also be written as "FILE" if you
379 like that better) takes a string enclosed in double quotes (`"') that
380 specifies the file, where the data is written. You may specifiy the same
381 file several times, in that case the data for all memory areas having this
382 file name is written into this file, in the order of the memory areas
383 defined in the MEMORY section. Let's specify some file names in the MEMORY
387 RAM1: start = $0800, size = $9800, file = %O;
388 ROM1: start = $A000, size = $2000, file = "rom1.bin";
389 RAM2: start = $C000, size = $1000, file = %O;
390 ROM2: start = $E000, size = $2000, file = "rom2.bin";
393 The %O used here is a way to specify the default behaviour explicitly: %O
394 is replaced by a string (including the quotes) that contains the default
395 output name, that is, "a.out" or the name specified with the -o option on
396 the command line. Into this file, the linker will first write any segments
397 that go into RAM1, and will append then the segments for RAM2, because the
398 memory areas are given in this order. So, for the RAM areas, nothing has
401 We've not used the ROM areas, but we will do that below, so we give the
402 file names here. Segments that go into ROM1 will be written to a file
403 named "rom1.bin", and segments that go into ROM2 will be written to a file
404 named "rom2.bin". The name given on the command line is ignored in both
407 Let us look now at a more complex example. Say, you've successfully tested
408 your new "Super Operating System" (SOS for short) for the C64, and you
409 will now go and replace the ROMs by your own code. When doing that, you
410 face a new problem: If the code runs in RAM, we need not to care about
411 read/write data. But now, if the code is in ROM, we must care about it.
412 Remember the default segments (you may of course specify your own):
415 RODATA read only data
417 BSS uninitialized data, read/write
419 Since the BSS is not initialized, we must not care about it now, but what
420 about DATA? DATA contains initialized data, that is, data that was
421 explicitly assigned a value. And your program will rely on these values on
422 startup. Since there's no other way to remember the contents of the data
423 segment, than storing it into one of the ROMs, we have to put it there.
424 But unfortunately, ROM is not writeable, so we have to copy it into RAM
425 before running the actual code.
427 The linker cannot help you copying the data from ROM into RAM (this must
428 be done by the startup code of your program), but it has some features
429 that will help you in this process.
431 First, you may not only specify a "load" attribute for a segment, but also
432 a "run" attribute. The "load" attribute is mandatory, and, if you don't
433 specify a "run" attribute, the linker assumes that load area and run area
434 are the same. We will use this feature for our data area:
437 CODE: load = ROM1, type = ro;
438 RODATA: load = ROM2, type = ro;
439 DATA: load = ROM2, run = RAM2, type = rw, define = yes;
440 BSS: load = RAM2, type = bss, define = yes;
443 Let's have a closer look at this SEGMENTS section. We specify that the
444 CODE segment goes into ROM1 (the one at $A000). The readonly data goes
445 into ROM2. Read/write data will be loaded into ROM2 but is run in RAM2.
446 That means that all references to labels in the DATA segment are relocated
447 to be in RAM2, but the segment is written to ROM2. All your startup code
448 has to do is, to copy the data from it's location in ROM2 to the final
451 So, how do you know, where the data is located? This is the second point,
452 where you get help from the linker. Remember the "define" attribute? Since
453 we have set this attribute to true, the linker will define three external
454 symbols for the data segment that may be accessed from your code:
456 __DATA_LOAD__ This is set to the address where the segment is
457 loaded, in this case, it is an address in ROM2.
458 __DATA_RUN__ This is set to the run address of the segment, in
459 this case, it is an address in RAM2.
460 __DATA_SIZE__ This is set to the segment size.
462 So, what your startup code must do, is to copy __DATA_SIZE__ bytes from
463 __DATA_LOAD__ to __DATA_RUN__ before any other routines are called. All
464 references to labels in the DATA segment are relocated to RAM2 by the
465 linker, so things will work properly.
467 There are some other attributes not covered above. Before starting the
468 reference section, I will discuss the remaining things here.
470 You may request symbols definitions also for memory areas. This may be
471 useful for things like a software stack, or an i/o area.
474 STACK: start = $C000, size = $1000, define = yes;
477 This will define three external symbols that may be used in your code:
479 __STACK_START__ This is set to the start of the memory
480 area, $C000 in this example.
482 __STACK_SIZE__ The size of the area, here $1000.
485 __STACK_LAST__ This is NOT the same as START+SIZE.
486 Instead, it it defined as the first
487 address that is not used by data. If we
488 don't define any segments for this area,
489 the value will be the same as START.
491 A memory section may also have a type. Valid types are
493 ro for readonly memory
494 and rw for read/write memory.
496 The linker will assure, that no segment marked as read/write or bss is put
497 into a memory area that is marked as readonly.
499 Unused memory in a memory area may be filled. Use the "fill = yes"
500 attribute to request this. The default value to fill unused space is zero.
501 If you don't like this, you may specify a byte value that is used to fill
502 these areas with the "fillval" attribute. This value is also used to fill
503 unfilled areas generated by the assemblers .ALIGN and .RES directives.
505 Segments may be aligned to some memory boundary. Specify "align = num" to
506 request this feature. Num must be a power of two. To align all segments on
510 CODE: load = ROM1, type = ro, align = $100;
511 RODATA: load = ROM2, type = ro, align = $100;
512 DATA: load = ROM2, run = RAM2, type = rw, define = yes,
514 BSS: load = RAM2, type = bss, define = yes, align = $100;
517 If an alignment is requested, the linker will add enough space to the
518 output file, so that the new segment starts at an address that is
519 divideable by the given number without a remainder. All addresses are
520 adjusted accordingly. To fill the unused space, bytes of zero are used,
521 or, if the memory area has a "fillval" attribute, that value. Alignment is
522 always needed, if you have the used the .ALIGN command in the assembler.
523 The alignment of a segment must be equal or greater than the alignment
524 used in the .ALIGN command. The linker will check that, and issue a
525 warning, if the alignment of a segment is lower than the alignment
526 requested in a .ALIGN command of one of the modules making up this
529 For a given segment you may also specify a fixed offset into a memory area or
530 a fixed start address. Use this if you want the code to run at a specific
531 address (a prominent case is the interrupt vector table which must go at
532 address $FFFA). Only one of ALIGN or OFFSET or START may be specified. If the
533 directive creates empty space, it will be filled with zero, of with the value
534 specified with the "fillval" attribute if one is given. The linker will warn
535 you if it is not possible to put the code at the specified offset (this may
536 happen if other segments in this area are too large). Here's an example:
539 VECTORS: load = ROM2, type = ro, start = $FFFA;
542 or (for the segment definitions from above)
545 VECTORS: load = ROM2, type = ro, offset = $1FFA;
548 File names may be empty, data from segments assigned to a memory area with
549 an empty file name is discarded. This is useful, if the a memory area has
550 segments assigned that are empty (for example because they are of type
551 bss). In that case, the linker will create an empty output file. This may
552 be suppressed by assigning an empty file name to that memory area.
554 The symbol %S may be used to access the default start address (that is,
555 $200 or the value given on the command line with the -S option).
564 4.3 Builtin configurations
565 --------------------------
567 Here is a list of the builin configurations for the different target
572 RAM: start = %S, size = $10000, file = %O;
575 CODE: load = RAM, type = rw;
576 RODATA: load = RAM, type = rw;
577 DATA: load = RAM, type = rw;
578 BSS: load = RAM, type = bss, define = yes;
583 HEADER: start = $0000, size = $6, file = %O;
584 RAM: start = $1F00, size = $6100, file = %O;
587 EXEHDR: load = HEADER, type = wprot;
588 CODE: load = RAM, type = wprot, define = yes;
589 RODATA: load = RAM, type = wprot;
590 DATA: load = RAM, type = rw;
591 BSS: load = RAM, type = bss, define = yes;
592 AUTOSTRT: load = RAM, type = wprot;
597 RAM: start = $7FF, size = $c801, file = %O;
600 CODE: load = RAM, type = ro;
601 RODATA: load = RAM, type = ro;
602 DATA: load = RAM, type = rw;
603 BSS: load = RAM, type = bss, define = yes;
608 RAM: start = $1bff, size = $a401, file = %O;
611 CODE: load = RAM, type = ro;
612 RODATA: load = RAM, type = ro;
613 DATA: load = RAM, type = rw;
614 BSS: load = RAM, type = bss, define = yes;
622 RAM: start = $0fff, size = $7001, file = %O;
625 CODE: load = RAM, type = ro;
626 RODATA: load = RAM, type = ro;
627 DATA: load = RAM, type = rw;
628 BSS: load = RAM, type = bss, define = yes;
633 RAM: start = $0001, size = $FFF0, file = %O;
636 CODE: load = RAM, type = ro;
637 RODATA: load = RAM, type = ro;
638 DATA: load = RAM, type = rw;
639 BSS: load = RAM, type = bss, define = yes;
644 RAM: start = $03FF, size = $7BFF, file = %O;
647 CODE: load = RAM, type = ro;
648 RODATA: load = RAM, type = ro;
649 DATA: load = RAM, type = rw;
650 BSS: load = RAM, type = bss, define = yes;
655 RAM: start = $800, size = $8E00, file = %O;
658 CODE: load = RAM, type = ro;
659 RODATA: load = RAM, type = ro;
660 DATA: load = RAM, type = rw;
661 BSS: load = RAM, type = bss, define = yes;
666 HEADER: start = $204, size = 508, file = %O;
667 RAM: start = $400, size = $7C00, file = %O;
670 HEADER: load = HEADER, type = ro;
671 CODE: load = RAM, type = ro;
672 RODATA: load = RAM, type = ro;
673 DATA: load = RAM, type = rw;
674 BSS: load = RAM, type = bss, define = yes;
677 The "start" attribute for the RAM memory area of the CBM systems is two
678 less than the actual start of the basic RAM to account for the two bytes
679 load address that is needed on disk and supplied by the startup code.
686 If you have problems using the linker, if you find any bugs, or if you're
687 doing something interesting with it, I would be glad to hear from you.
688 Feel free to contact me by email (uz@musoftware.de).
695 ld65 (and all cc65 binutils) are (C) Copyright 1998-2000 Ullrich von
696 Bassewitz. For usage of the binaries and/or sources the following
699 This software is provided 'as-is', without any expressed or implied
700 warranty. In no event will the authors be held liable for any damages
701 arising from the use of this software.
703 Permission is granted to anyone to use this software for any purpose,
704 including commercial applications, and to alter it and redistribute it
705 freely, subject to the following restrictions:
707 1. The origin of this software must not be misrepresented; you must not
708 claim that you wrote the original software. If you use this software
709 in a product, an acknowledgment in the product documentation would be
710 appreciated but is not required.
711 2. Altered source versions must be plainly marked as such, and must not
712 be misrepresented as being the original software.
713 3. This notice may not be removed or altered from any source