]> git.sur5r.net Git - cc65/blob - doc/ld65.txt
Added --feature and -target
[cc65] / doc / ld65.txt
1
2
3                                    ld65
4
5                      A Linker for ca65 Object modules
6
7               (C) Copyright 1998-2000 Ullrich von Bassewitz
8                             (uz@musoftware.de)
9
10
11
12 Contents
13 --------
14
15   1. Overview
16
17   2. Usage
18
19   3. Detailed workings
20
21   4. Output configuration files
22   4.1 Introduction
23   4.2 Reference
24   4.3 Builtin configurations
25
26   5. Bugs/Feedback
27
28   6. Copyright
29
30
31
32 1. Overview
33 -----------
34
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:
40
41   * Accept any number of segments to form an executable module.
42
43   * Resolve arbitrary expressions stored in the object files.
44
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.
50
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.
56
57
58
59 2. Usage
60 --------
61
62 The linker is called as follows:
63
64 ---------------------------------------------------------------------------
65 Usage: ld65 [options] module ...
66 Short options:
67   -h                    Help (this text)
68   -m name               Create a map file
69   -o name               Name the default output file
70   -t sys                Set the target system
71   -v                    Verbose mode
72   -vm                   Verbose map file
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
78
79 Long options:
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 ---------------------------------------------------------------------------
85
86
87   -h
88   --help
89
90   Print the short option summary shown above.
91
92
93   -m name
94   --mapfile name
95
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
100   symbols.
101
102
103   -o name
104
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.
109
110   -t sys
111   --target sys
112
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
116   supported:
117
118         none
119         atari
120         c64
121         c128
122         plus4
123         cbm610
124         pet
125         apple2
126         geos
127
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
130   configurations.
131
132
133   -v
134   --verbose
135
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.
140
141
142   -vm
143
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.
148
149
150   -C
151
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
154   -t.
155
156
157   -Ln
158
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.
164
165
166   -Lp
167
168   Deprecated option.
169
170
171   -S addr
172   --start-addr addr
173
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.
178
179
180   -V
181   --version
182
183   This option print the version number of the linker. If you send any
184   suggestions or bugfixes, please include this number.
185
186
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.
191
192
193
194 3. Detailed workings
195 --------------------
196
197 The linker does several things when combining object modules:
198
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.
208
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
212
213         ld65 crt0.o clib.lib test.o
214
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
217 on the command line:
218
219         ld65 crt0.o test.o clib.lib
220
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).
223
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).
229
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).
234
235 Step five is to output a map file with a detailed list of all modules,
236 segments and symbols encountered.
237
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
240 developer:-)
241
242
243
244 4. Output configuration files
245 -----------------------------
246
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.
251
252 Case is ignored for keywords, that is, section or attribute names, but it
253 is NOT ignored for names and strings.
254
255
256
257 4.1 Introduction
258 ----------------
259
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):
262
263         MEMORY {
264             RAM1:  start = $0800, size = $9800;
265             ROM1:  start = $A000, size = $2000;
266             RAM2:  start = $C000, size = $1000;
267             ROM2:  start = $E000, size = $2000;
268         }
269
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.
275
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:
283
284         # Start of memory section
285         MEMORY
286         {
287             RAM1:
288                 start $0800
289                 size $9800;
290             ROM1:
291                 start $A000
292                 size $2000;
293             RAM2:
294                 start $C000
295                 size $1000;
296             ROM2:
297                 start $E000
298                 size $2000;
299         }
300
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.
307
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
311 section:
312
313         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;
318         }
319
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:
326
327         ro      means readonly
328         wprot   same as ro but will be marked as write protected in
329                 the VICE label file if -Lp is given
330         rw      means read/write
331         bss     means that this is an uninitialized segment
332         empty   will not go in any output file
333
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.
341
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.
345
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
353 three symbols.
354
355         __NAME_LOAD__   This is set to the address where the segment is
356                         loaded.
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.
360
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.
363
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?
368
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.
377
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
384 section used above:
385
386         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";
391         }
392
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
399 really changed.
400
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
405 cases.
406
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):
413
414         CODE            read only code
415         RODATA          read only data
416         DATA            read/write data
417         BSS             uninitialized data, read/write
418
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.
426
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.
430
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:
435
436         SEGMENTS {
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;
441         }
442
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
449 location in RAM2.
450
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:
455
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.
461
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.
466
467 There are some other attributes not covered above. Before starting the
468 reference section, I will discuss the remaining things here.
469
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.
472
473         MEMORY {
474             STACK:  start = $C000, size = $1000, define = yes;
475         }
476
477 This will define three external symbols that may be used in your code:
478
479         __STACK_START__         This is set to the start of the memory
480                                 area, $C000 in this example.
481
482         __STACK_SIZE__          The size of the area, here $1000.
483
484
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.
490
491 A memory section may also have a type. Valid types are
492
493         ro      for readonly memory
494 and     rw      for read/write memory.
495
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.
498
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.
504
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
507 a page boundary, use
508
509         SEGMENTS {
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,
513                     align = $100;
514             BSS:    load = RAM2, type = bss, define = yes, align = $100;
515         }
516
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
527 segment.
528
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:
537
538         SEGMENTS {
539             VECTORS: load = ROM2, type = ro, start = $FFFA;
540         }
541
542 or (for the segment definitions from above)
543
544         SEGMENTS {
545             VECTORS: load = ROM2, type = ro, offset = $1FFA;
546         }
547
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.
553
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).
556
557
558
559 4.2 Reference
560 -------------
561
562
563
564 4.3 Builtin configurations
565 --------------------------
566
567 Here is a list of the builin configurations for the different target
568 types:
569
570 none:
571         MEMORY {
572             RAM: start = %S, size = $10000, file = %O;
573         }
574         SEGMENTS {
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;
579         }
580
581 atari:
582         MEMORY {
583             HEADER: start = $0000, size = $6, file = %O;
584             RAM: start = $1F00, size = $6100, file = %O;
585         }
586         SEGMENTS {
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;
593         }
594
595 c64:
596         MEMORY {
597             RAM: start = $7FF, size = $c801, file = %O;
598         }
599         SEGMENTS {
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;
604         }
605
606 c128:
607         MEMORY {
608             RAM: start = $1bff, size = $a401, file = %O;
609         }
610         SEGMENTS {
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;
615         }
616
617 ace:
618         (non-existent)
619
620 plus4:
621         MEMORY {
622             RAM: start = $0fff, size = $7001, file = %O;
623         }
624         SEGMENTS {
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;
629         }
630
631 cbm610:
632         MEMORY {
633             RAM: start = $0001, size = $FFF0, file = %O;
634         }
635         SEGMENTS {
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;
640         }
641
642 pet:
643         MEMORY {
644             RAM: start = $03FF, size = $7BFF, file = %O;
645         }
646         SEGMENTS {
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;
651         }
652
653 apple2:
654         MEMORY {
655             RAM: start = $800, size = $8E00, file = %O;
656         }
657         SEGMENTS {
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;
662         }
663
664 geos:
665         MEMORY {
666             HEADER: start = $204, size = 508, file = %O;
667             RAM: start = $400, size = $7C00, file = %O;
668         }
669         SEGMENTS {
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;
675         }
676
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.
680
681
682
683 5. Bugs/Feedback
684 ----------------
685
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).
689
690
691
692 6. Copyright
693 ------------
694
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
697 conditions do apply:
698
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.
702
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:
706
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
714    distribution.
715
716
717
718