1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * ldrdf.c - RDOFF Object File linker/loader main program.
40 * - enhance search of required export symbols in libraries (now depends
41 * on modules order in library)
42 * - keep a cache of symbol names in each library module so
43 * we don't have to constantly recheck the file
44 * - general performance improvements
46 * BUGS & LIMITATIONS: this program doesn't support multiple code, data
47 * or bss segments, therefore for 16 bit programs whose code, data or BSS
48 * segment exceeds 64K in size, it will not work. This program probably
49 * won't work if compiled by a 16 bit compiler. Try DJGPP if you're running
50 * under DOS. '#define STINGY_MEMORY' may help a little.
65 #define LDRDF_VERSION "1.08"
67 /* #define STINGY_MEMORY */
69 /* =======================================================================
70 * Types & macros that are private to this program
73 struct segment_infonode
{
74 int dest_seg
; /* output segment to be placed into, -1 to
75 skip linking this segment */
76 int32_t reloc
; /* segment's relocation factor */
80 rdffile f
; /* the RDOFF file structure */
81 struct segment_infonode seginfo
[RDF_MAXSEGS
]; /* what are we doing
85 struct modulenode
*next
;
91 /* ==========================================================================
92 * Function prototypes of private utility functions
95 void processmodule(const char *filename
, struct modulenode
*mod
);
96 int allocnewseg(uint16_t type
, uint16_t reserved
);
97 int findsegment(uint16_t type
, uint16_t reserved
);
98 void symtab_add(const char *symbol
, int segment
, int32_t offset
);
99 int symtab_get(const char *symbol
, int *segment
, int32_t *offset
);
101 /* =========================================================================
102 * Global data structures.
105 /* a linked list of modules that will be included in the output */
106 struct modulenode
*modules
= NULL
;
107 struct modulenode
*lastmodule
= NULL
;
109 /* a linked list of libraries to be searched for unresolved imported symbols */
110 struct librarynode
*libraries
= NULL
;
111 struct librarynode
*lastlib
= NULL
;
113 /* the symbol table */
116 /* objects search path */
117 char *objpath
= NULL
;
119 /* libraries search path */
120 char *libpath
= NULL
;
122 /* file to embed as a generic record */
123 char *generic_rec_file
= NULL
;
125 /* module name to be added at the beginning of output file */
126 char *modname_specified
= NULL
;
128 /* the header of the output file, built up stage by stage */
129 rdf_headerbuf
*newheader
= NULL
;
131 /* The current state of segment allocation, including information about
132 * which output segment numbers have been allocated, and their types and
133 * amount of data which has already been allocated inside them.
135 struct SegmentHeaderRec outputseg
[RDF_MAXSEGS
];
139 /* global options which affect how the program behaves */
140 struct ldrdfoptions
{
151 int errorcount
= 0; /* determines main program exit status */
153 /* =========================================================================
160 * sets up segments 0, 1, and 2, the initial code data and bss segments
162 static void initsegments(void)
165 outputseg
[0].type
= 1;
166 outputseg
[0].number
= 0;
167 outputseg
[0].reserved
= 0;
168 outputseg
[0].length
= 0;
169 outputseg
[1].type
= 2;
170 outputseg
[1].number
= 1;
171 outputseg
[1].reserved
= 0;
172 outputseg
[1].length
= 0;
173 outputseg
[2].type
= 0xFFFF; /* reserved segment type */
174 outputseg
[2].number
= 2;
175 outputseg
[2].reserved
= 0;
176 outputseg
[2].length
= 0;
183 * Determine the characteristics of a module, and decide what to do with
184 * each segment it contains (including determining destination segments and
185 * relocation factors for segments that are kept).
187 static void loadmodule(const char *filename
)
190 printf("loading `%s'\n", filename
);
192 /* allocate a new module entry on the end of the modules list */
194 modules
= nasm_malloc(sizeof(*modules
));
195 lastmodule
= modules
;
197 lastmodule
->next
= nasm_malloc(sizeof(*modules
));
198 lastmodule
= lastmodule
->next
;
202 fprintf(stderr
, "ldrdf: out of memory\n");
206 /* open the file using 'rdfopen', which returns nonzero on error */
207 if (rdfopen(&lastmodule
->f
, filename
) != 0) {
208 rdfperror("ldrdf", filename
);
213 * store information about the module, and determine what segments
214 * it contains, and what we should do with them (determine relocation
215 * factor if we decide to keep them)
217 lastmodule
->header
= NULL
;
218 lastmodule
->name
= nasm_strdup(filename
);
219 lastmodule
->next
= NULL
;
221 processmodule(filename
, lastmodule
);
227 * step through each segment, determine what exactly we're doing with
228 * it, and if we intend to keep it, determine (a) which segment to
229 * put it in and (b) whereabouts in that segment it will end up.
230 * (b) is fairly easy, because we're now keeping track of how big each
231 * segment in our output file is...
233 void processmodule(const char *filename
, struct modulenode
*mod
)
235 struct segconfig sconf
;
239 int32_t bssamount
= 0;
240 int bss_was_referenced
= 0;
242 memset(&sconf
, 0, sizeof sconf
);
244 for (seg
= 0; seg
< mod
->f
.nsegs
; seg
++) {
246 * get the segment configuration for this type from the segment
247 * table. getsegconfig() is a macro, defined in ldsegs.h.
249 getsegconfig(sconf
, mod
->f
.seg
[seg
].type
);
251 if (options
.verbose
> 1) {
252 printf("%s %04x [%04x:%10s] ", filename
,
253 mod
->f
.seg
[seg
].number
, mod
->f
.seg
[seg
].type
,
257 * sconf->dowhat tells us what to do with a segment of this type.
259 switch (sconf
.dowhat
) {
262 * Set destination segment to -1, to indicate that this segment
263 * should be ignored for the purpose of output, ie it is left
264 * out of the linked executable.
266 mod
->seginfo
[seg
].dest_seg
= -1;
267 if (options
.verbose
> 1)
273 * The configuration tells us to create a new segment for
274 * each occurrence of this segment type.
276 outseg
= allocnewseg(sconf
.mergetype
,
277 mod
->f
.seg
[seg
].reserved
);
278 mod
->seginfo
[seg
].dest_seg
= outseg
;
279 mod
->seginfo
[seg
].reloc
= 0;
280 outputseg
[outseg
].length
= mod
->f
.seg
[seg
].length
;
281 if (options
.verbose
> 1)
282 printf("=> %04x:%08"PRIx32
" (+%04"PRIx32
")\n", outseg
,
283 mod
->seginfo
[seg
].reloc
, mod
->f
.seg
[seg
].length
);
288 * The configuration tells us to merge the segment with
289 * a previously existing segment of type 'sconf.mergetype',
290 * if one exists. Otherwise a new segment is created.
291 * This is handled transparently by 'findsegment()'.
293 outseg
= findsegment(sconf
.mergetype
,
294 mod
->f
.seg
[seg
].reserved
);
295 mod
->seginfo
[seg
].dest_seg
= outseg
;
298 * We need to add alignment to these segments.
300 if (outputseg
[outseg
].length
% options
.align
!= 0)
301 outputseg
[outseg
].length
+=
303 (outputseg
[outseg
].length
% options
.align
);
305 mod
->seginfo
[seg
].reloc
= outputseg
[outseg
].length
;
306 outputseg
[outseg
].length
+= mod
->f
.seg
[seg
].length
;
308 if (options
.verbose
> 1)
309 printf("=> %04x:%08"PRIx32
" (+%04"PRIx32
")\n", outseg
,
310 mod
->seginfo
[seg
].reloc
, mod
->f
.seg
[seg
].length
);
316 * extract symbols from the header, and dump them into the
319 header
= nasm_malloc(mod
->f
.header_len
);
321 fprintf(stderr
, "ldrdf: not enough memory\n");
324 if (rdfloadseg(&mod
->f
, RDOFF_HEADER
, header
)) {
325 rdfperror("ldrdf", filename
);
329 while ((hr
= rdfgetheaderrec(&mod
->f
))) {
331 case RDFREC_IMPORT
: /* imported symbol */
332 case RDFREC_FARIMPORT
:
333 /* Define with seg = -1 */
334 symtab_add(hr
->i
.label
, -1, 0);
337 case RDFREC_GLOBAL
:{ /* exported symbol */
341 if (hr
->e
.segment
== 2) {
342 bss_was_referenced
= 1;
343 destreloc
= bss_length
;
344 if (destreloc
% options
.align
!= 0)
346 options
.align
- (destreloc
% options
.align
);
350 mod
->seginfo
[(int)hr
->e
.segment
].dest_seg
) == -1)
352 destreloc
= mod
->seginfo
[(int)hr
->e
.segment
].reloc
;
354 symtab_add(hr
->e
.label
, destseg
, destreloc
+ hr
->e
.offset
);
358 case RDFREC_BSS
: /* BSS reservation */
360 * first, amalgamate all BSS reservations in this module
361 * into one, because we allow this in the output format.
363 bssamount
+= hr
->b
.amount
;
366 case RDFREC_COMMON
:{ /* Common variable */
367 symtabEnt
*ste
= symtabFind(symtab
, hr
->c
.label
);
369 /* Is the symbol already in the table? */
373 /* Align the variable */
374 if (bss_length
% hr
->c
.align
!= 0)
375 bss_length
+= hr
->c
.align
- (bss_length
% hr
->c
.align
);
376 if (options
.verbose
> 1) {
377 printf("%s %04x common '%s' => 0002:%08"PRIx32
" (+%04"PRIx32
")\n",
378 filename
, hr
->c
.segment
, hr
->c
.label
,
379 bss_length
, hr
->c
.size
);
382 symtab_add(hr
->c
.label
, 2, bss_length
);
383 mod
->bss_reloc
= bss_length
;
384 bss_length
+= hr
->c
.size
;
390 if (bssamount
!= 0 || bss_was_referenced
) {
392 * handle the BSS segment - first pad the existing bss length
393 * to the correct alignment, then store the length in bss_reloc
394 * for this module. Then add this module's BSS length onto
397 if (bss_length
% options
.align
!= 0)
398 bss_length
+= options
.align
- (bss_length
% options
.align
);
400 mod
->bss_reloc
= bss_length
;
401 if (options
.verbose
> 1) {
402 printf("%s 0002 [ BSS] => 0002:%08"PRIx32
" (+%04"PRIx32
")\n",
403 filename
, bss_length
, bssamount
);
405 bss_length
+= bssamount
;
409 * we free the header buffer here, to save memory later.
410 * this isn't efficient, but probably halves the memory usage
413 mod
->f
.header_loc
= NULL
;
421 * Return 1 if a given module is in the list, 0 otherwise.
423 static int lookformodule(const char *name
)
425 struct modulenode
*curr
= modules
;
428 if (!strcmp(name
, curr
->name
))
439 * These functions manipulate the array of output segments, and are used
440 * by processmodule(). allocnewseg() allocates a segment in the array,
441 * initialising it to be empty. findsegment() first scans the array for
442 * a segment of the type requested, and if one isn't found allocates a
445 int allocnewseg(uint16_t type
, uint16_t reserved
)
447 outputseg
[nsegs
].type
= type
;
448 outputseg
[nsegs
].number
= nsegs
;
449 outputseg
[nsegs
].reserved
= reserved
;
450 outputseg
[nsegs
].length
= 0;
451 outputseg
[nsegs
].offset
= 0;
452 outputseg
[nsegs
].data
= NULL
;
457 int findsegment(uint16_t type
, uint16_t reserved
)
461 for (i
= 0; i
< nsegs
; i
++)
462 if (outputseg
[i
].type
== type
)
465 return allocnewseg(type
, reserved
);
471 * inserts a symbol into the global symbol table, which associates symbol
472 * names either with addresses, or a marker that the symbol hasn't been
473 * resolved yet, or possibly that the symbol has been defined as
474 * contained in a dynamic [load time/run time] linked library.
476 * segment = -1 => not yet defined
477 * segment = -2 => defined as dll symbol
479 * If the symbol is already defined, and the new segment >= 0, then
480 * if the original segment was < 0 the symbol is redefined, otherwise
481 * a duplicate symbol warning is issued. If new segment == -1, this
482 * routine won't change a previously existing symbol. It will change
483 * to segment = -2 only if the segment was previously < 0.
485 void symtab_add(const char *symbol
, int segment
, int32_t offset
)
489 ste
= symtabFind(symtab
, symbol
);
491 if (ste
->segment
>= 0) {
493 * symbol previously defined
497 fprintf(error_file
, "warning: `%s' redefined\n", symbol
);
502 * somebody wanted the symbol, and put an undefined symbol
503 * marker into the table
508 * we have more information now - update the symbol's entry
510 ste
->segment
= segment
;
511 ste
->offset
= offset
;
516 * this is the first declaration of this symbol
518 ste
= nasm_malloc(sizeof(symtabEnt
));
520 fprintf(stderr
, "ldrdf: out of memory\n");
523 ste
->name
= nasm_strdup(symbol
);
524 ste
->segment
= segment
;
525 ste
->offset
= offset
;
527 symtabInsert(symtab
, ste
);
533 * Retrieves the values associated with a symbol. Undefined symbols
534 * are assumed to have -1:0 associated. Returns 1 if the symbol was
535 * successfully located.
537 int symtab_get(const char *symbol
, int *segment
, int32_t *offset
)
539 symtabEnt
*ste
= symtabFind(symtab
, symbol
);
545 *segment
= ste
->segment
;
546 *offset
= ste
->offset
;
554 * checks that a library can be opened and is in the correct format,
555 * then adds it to the linked list of libraries.
557 static void add_library(const char *name
)
559 if (rdl_verify(name
)) {
560 rdl_perror("ldrdf", name
);
565 lastlib
= libraries
= nasm_malloc(sizeof(*libraries
));
567 fprintf(stderr
, "ldrdf: out of memory\n");
571 lastlib
->next
= nasm_malloc(sizeof(*libraries
));
572 if (!lastlib
->next
) {
573 fprintf(stderr
, "ldrdf: out of memory\n");
576 lastlib
= lastlib
->next
;
578 lastlib
->next
= NULL
;
579 if (rdl_open(lastlib
, name
)) {
580 rdl_perror("ldrdf", name
);
589 * scans through the list of libraries, attempting to match symbols
590 * defined in library modules against symbols that are referenced but
591 * not defined (segment = -1 in the symbol table)
593 * returns 1 if any extra library modules are included, indicating that
594 * another pass through the library list should be made (possibly).
596 static int search_libraries(void)
598 struct librarynode
*cur
;
604 int doneanything
= 0, pass
= 1, keepfile
;
610 if (options
.verbose
> 2)
611 printf("scanning library `%s', pass %d...\n", cur
->name
, pass
);
613 for (i
= 0; rdl_openmodule(cur
, i
, &f
) == 0; i
++) {
614 if (pass
== 2 && lookformodule(f
.name
))
617 if (options
.verbose
> 3)
618 printf(" looking in module `%s'\n", f
.name
);
620 header
= nasm_malloc(f
.header_len
);
622 fprintf(stderr
, "ldrdf: not enough memory\n");
625 if (rdfloadseg(&f
, RDOFF_HEADER
, header
)) {
626 rdfperror("ldrdf", f
.name
);
633 while ((hr
= rdfgetheaderrec(&f
))) {
634 /* We're only interested in exports, so skip others */
635 if (hr
->type
!= RDFREC_GLOBAL
)
639 * If the symbol is marked as SYM_GLOBAL, somebody will be
640 * definitely interested in it..
642 if ((hr
->e
.flags
& SYM_GLOBAL
) == 0) {
644 * otherwise the symbol is just public. Find it in
645 * the symbol table. If the symbol isn't defined, we
646 * aren't interested, so go on to the next.
647 * If it is defined as anything but -1, we're also not
648 * interested. But if it is defined as -1, insert this
649 * module into the list of modules to use, and go
650 * immediately on to the next module...
652 if (!symtab_get(hr
->e
.label
, &segment
, &offset
)
661 * as there are undefined symbols, we can assume that
662 * there are modules on the module list by the time
665 lastmodule
->next
= nasm_malloc(sizeof(*lastmodule
->next
));
666 if (!lastmodule
->next
) {
667 fprintf(stderr
, "ldrdf: not enough memory\n");
670 lastmodule
= lastmodule
->next
;
671 memcpy(&lastmodule
->f
, &f
, sizeof(f
));
672 lastmodule
->name
= nasm_strdup(f
.name
);
673 lastmodule
->next
= NULL
;
674 processmodule(f
.name
, lastmodule
);
683 if (rdl_error
!= 0 && rdl_error
!= RDL_ENOTFOUND
)
684 rdl_perror("ldrdf", cur
->name
);
687 if (cur
== NULL
&& pass
== 1) {
699 * this takes the linked list of modules, and walks through it, merging
700 * all the modules into a single output module, and then writes this to a
703 static void write_output(const char *filename
)
706 rdf_headerbuf
*rdfheader
;
707 struct modulenode
*cur
;
708 int i
, n
, availableseg
, seg
, localseg
, isrelative
;
710 rdfheaderrec
*hr
, newrec
;
716 if ((f
= fopen(filename
, "wb")) == NULL
) {
717 fprintf(stderr
, "ldrdf: couldn't open %s for output\n", filename
);
720 if ((rdfheader
= rdfnewheader()) == NULL
) {
721 fprintf(stderr
, "ldrdf: out of memory\n");
726 * If '-g' option was given, first record in output file will be a
727 * `generic' record, filled with a given file content.
728 * This can be useful, for example, when constructing multiboot
731 if (generic_rec_file
) {
735 printf("\nadding generic record from binary file %s\n",
738 hr
= (rdfheaderrec
*) nasm_malloc(sizeof(struct GenericRec
));
739 if ((ff
= fopen(generic_rec_file
, "r")) == NULL
) {
740 fprintf(stderr
, "ldrdf: couldn't open %s for input\n",
744 n
= fread(hr
->g
.data
, 1, sizeof(hr
->g
.data
), ff
);
745 fseek(ff
, 0, SEEK_END
);
746 if (ftell(ff
) > (long)sizeof(hr
->g
.data
)) {
748 "warning: maximum generic record size is %u, "
749 "rest of file ignored\n",
750 (unsigned int)sizeof(hr
->g
.data
));
754 hr
->g
.type
= RDFREC_GENERIC
;
756 rdfaddheader(rdfheader
, hr
);
761 * Add module name record if `-mn' option was given
763 if (modname_specified
) {
764 n
= strlen(modname_specified
);
766 if ((n
< 1) || (n
>= MODLIB_NAME_MAX
)) {
767 fprintf(stderr
, "ldrdf: invalid length of module name `%s'\n",
773 printf("\nadding module name record %s\n", modname_specified
);
775 hr
= (rdfheaderrec
*) nasm_malloc(sizeof(struct ModRec
));
776 hr
->m
.type
= RDFREC_MODNAME
;
777 hr
->m
.reclen
= n
+ 1;
778 strcpy(hr
->m
.modname
, modname_specified
);
779 rdfaddheader(rdfheader
, hr
);
785 printf("\nbuilding output module (%d segments)\n", nsegs
);
788 * Allocate the memory for the segments. We may be better off
789 * building the output module one segment at a time when running
790 * under 16 bit DOS, but that would be a slower way of doing this.
791 * And you could always use DJGPP...
793 for (i
= 0; i
< nsegs
; i
++) {
794 outputseg
[i
].data
= NULL
;
795 if (!outputseg
[i
].length
)
797 outputseg
[i
].data
= nasm_malloc(outputseg
[i
].length
);
798 if (!outputseg
[i
].data
) {
799 fprintf(stderr
, "ldrdf: out of memory\n");
805 * initialise availableseg, used to allocate segment numbers for
806 * imported and exported labels...
808 availableseg
= nsegs
;
811 * Step through the modules, performing required actions on each one
813 for (cur
= modules
; cur
; cur
= cur
->next
) {
815 * Read the actual segment contents into the correct places in
816 * the newly allocated segments
819 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
820 int dest
= cur
->seginfo
[i
].dest_seg
;
824 if (rdfloadseg(&cur
->f
, i
,
825 outputseg
[dest
].data
+ cur
->seginfo
[i
].reloc
)) {
826 rdfperror("ldrdf", cur
->name
);
832 * Perform fixups, and add new header records where required
835 header
= nasm_malloc(cur
->f
.header_len
);
837 fprintf(stderr
, "ldrdf: out of memory\n");
841 if (cur
->f
.header_loc
)
842 rdfheaderrewind(&cur
->f
);
843 else if (rdfloadseg(&cur
->f
, RDOFF_HEADER
, header
)) {
844 rdfperror("ldrdf", cur
->name
);
849 * we need to create a local segment number -> location
850 * table for the segments in this module.
852 init_seglocations(&segs
);
853 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
854 add_seglocation(&segs
, cur
->f
.seg
[i
].number
,
855 cur
->seginfo
[i
].dest_seg
,
856 cur
->seginfo
[i
].reloc
);
859 * and the BSS segment (doh!)
861 add_seglocation(&segs
, 2, 2, cur
->bss_reloc
);
863 while ((hr
= rdfgetheaderrec(&cur
->f
))) {
865 case RDFREC_RELOC
: /* relocation record - need to do a fixup */
867 * First correct the offset stored in the segment from
868 * the start of the segment (which may well have changed).
870 * To do this we add to the number stored the relocation
871 * factor associated with the segment that contains the
874 * The relocation could be a relative relocation, in which
875 * case we have to first subtract the amount we've relocated
876 * the containing segment by.
878 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
880 "%s: reloc to undefined segment %04x\n",
881 cur
->name
, (int)hr
->r
.refseg
);
887 (hr
->r
.segment
& RDOFF_RELATIVEMASK
) ==
889 hr
->r
.segment
&= (RDOFF_RELATIVEMASK
- 1);
891 if (hr
->r
.segment
== 2 ||
893 rdffindsegment(&cur
->f
, hr
->r
.segment
)) == -1) {
894 fprintf(stderr
, "%s: reloc from %s segment (%d)\n",
896 hr
->r
.segment
== 2 ? "BSS" : "unknown",
902 if (hr
->r
.length
!= 1 && hr
->r
.length
!= 2 &&
904 fprintf(stderr
, "%s: nonstandard length reloc "
905 "(%d bytes)\n", cur
->name
, hr
->r
.length
);
911 * okay, now the relocation is in the segment pointed to by
912 * cur->seginfo[localseg], and we know everything else is
913 * okay to go ahead and do the relocation
915 data
= outputseg
[cur
->seginfo
[localseg
].dest_seg
].data
;
916 data
+= cur
->seginfo
[localseg
].reloc
+ hr
->r
.offset
;
919 * data now points to the reference that needs
920 * relocation. Calculate the relocation factor.
922 * offset of referred object in segment [in offset]
923 * (- relocation of localseg, if ref is relative)
924 * For simplicity, the result is stored in 'offset'.
925 * Then add 'offset' onto the value at data.
929 offset
-= cur
->seginfo
[localseg
].reloc
;
930 switch (hr
->r
.length
) {
933 if (offset
< -127 || offset
> 128)
935 "warning: relocation out of range "
936 "at %s(%02x:%08"PRIx32
")\n", cur
->name
,
937 (int)hr
->r
.segment
, hr
->r
.offset
);
938 *data
= (char)offset
;
941 offset
+= *(int16_t *)data
;
942 if (offset
< -32767 || offset
> 32768)
944 "warning: relocation out of range "
945 "at %s(%02x:%08"PRIx32
")\n", cur
->name
,
946 (int)hr
->r
.segment
, hr
->r
.offset
);
947 *(int16_t *)data
= (int16_t)offset
;
950 *(int32_t *)data
+= offset
;
951 /* we can't easily detect overflow on this one */
956 * If the relocation was relative between two symbols in
957 * the same segment, then we're done.
959 * Otherwise, we need to output a new relocation record
960 * with the references updated segment and offset...
962 if (!isrelative
|| cur
->seginfo
[localseg
].dest_seg
!= seg
) {
963 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
964 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
967 hr
->r
.segment
+= RDOFF_RELATIVEMASK
;
968 rdfaddheader(rdfheader
, hr
);
972 case RDFREC_IMPORT
: /* import symbol */
973 case RDFREC_FARIMPORT
:
975 * scan the global symbol table for the symbol
976 * and associate its location with the segment number
979 se
= symtabFind(symtab
, hr
->i
.label
);
980 if (!se
|| se
->segment
== -1) {
981 if (!options
.dynalink
&& !(hr
->i
.flags
& SYM_IMPORT
)) {
983 "error: unresolved reference to `%s'"
984 " in module `%s'\n", hr
->i
.label
,
989 * we need to allocate a segment number for this
990 * symbol, and store it in the symbol table for
994 se
= nasm_malloc(sizeof(*se
));
996 fprintf(stderr
, "ldrdf: out of memory\n");
999 se
->name
= nasm_strdup(hr
->i
.label
);
1001 se
->segment
= availableseg
++;
1003 symtabInsert(symtab
, se
);
1005 se
->segment
= availableseg
++;
1009 * output a header record that imports it to the
1010 * recently allocated segment number...
1013 newrec
.i
.segment
= se
->segment
;
1014 rdfaddheader(rdfheader
, &newrec
);
1017 add_seglocation(&segs
, hr
->i
.segment
, se
->segment
,
1021 case RDFREC_GLOBAL
: /* export symbol */
1023 * need to insert an export for this symbol into the new
1024 * header, unless we're stripping symbols. Even if we're
1025 * stripping, put the symbol if it's marked as SYM_GLOBAL.
1027 if (options
.strip
&& !(hr
->e
.flags
& SYM_GLOBAL
))
1030 if (hr
->e
.segment
== 2) {
1032 offset
= cur
->bss_reloc
;
1034 localseg
= rdffindsegment(&cur
->f
, hr
->e
.segment
);
1035 if (localseg
== -1) {
1036 fprintf(stderr
, "%s: exported symbol `%s' from "
1037 "unrecognised segment\n", cur
->name
,
1042 offset
= cur
->seginfo
[localseg
].reloc
;
1043 seg
= cur
->seginfo
[localseg
].dest_seg
;
1046 hr
->e
.segment
= seg
;
1047 hr
->e
.offset
+= offset
;
1048 rdfaddheader(rdfheader
, hr
);
1051 case RDFREC_MODNAME
: /* module name */
1053 * Insert module name record if export symbols
1055 * If module name begins with '$' - insert it anyway.
1057 if (options
.strip
&& hr
->m
.modname
[0] != '$')
1059 rdfaddheader(rdfheader
, hr
);
1062 case RDFREC_DLL
: /* DLL name */
1064 * Insert DLL name if it begins with '$'
1066 if (hr
->d
.libname
[0] != '$')
1068 rdfaddheader(rdfheader
, hr
);
1071 case RDFREC_SEGRELOC
: /* segment fixup */
1073 * modify the segment numbers if necessary, and
1074 * pass straight through to the output module header
1078 if (hr
->r
.segment
== 2) {
1079 fprintf(stderr
, "%s: segment fixup in BSS section\n",
1084 localseg
= rdffindsegment(&cur
->f
, hr
->r
.segment
);
1085 if (localseg
== -1) {
1086 fprintf(stderr
, "%s: segment fixup in unrecognised"
1087 " segment (%d)\n", cur
->name
, hr
->r
.segment
);
1091 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
1092 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
1094 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
1095 fprintf(stderr
, "%s: segment fixup to undefined "
1096 "segment %04x\n", cur
->name
,
1102 rdfaddheader(rdfheader
, hr
);
1105 case RDFREC_COMMON
: /* Common variable */
1106 /* Is this symbol already in the table? */
1107 se
= symtabFind(symtab
, hr
->c
.label
);
1109 printf("%s is not in symtab yet\n", hr
->c
.label
);
1112 /* Add segment location */
1113 add_seglocation(&segs
, hr
->c
.segment
, se
->segment
,
1120 done_seglocations(&segs
);
1125 * combined BSS reservation for the entire results
1127 newrec
.type
= RDFREC_BSS
;
1128 newrec
.b
.reclen
= 4;
1129 newrec
.b
.amount
= bss_length
;
1130 rdfaddheader(rdfheader
, &newrec
);
1135 for (i
= 0; i
< nsegs
; i
++) {
1138 rdfaddsegment(rdfheader
, outputseg
[i
].length
);
1141 rdfwriteheader(f
, rdfheader
);
1142 rdfdoneheader(rdfheader
);
1145 * Step through the segments, one at a time, writing out into
1148 for (i
= 0; i
< nsegs
; i
++) {
1152 fwriteint16_t(outputseg
[i
].type
, f
);
1153 fwriteint16_t(outputseg
[i
].number
, f
);
1154 fwriteint16_t(outputseg
[i
].reserved
, f
);
1155 fwriteint32_t(outputseg
[i
].length
, f
);
1156 nasm_write(outputseg
[i
].data
, outputseg
[i
].length
, f
);
1162 /* =========================================================================
1166 static void usage(void)
1169 " ldrdf [options] object modules ... [-llibrary ...]\n"
1172 " -v[=n] increase verbosity by 1, or set it to n\n"
1173 " -a nn set segment alignment value (default 16)\n"
1174 " -s strip public symbols\n"
1175 " -dy Unix-style dynamic linking\n"
1176 " -o name write output in file 'name'\n"
1177 " -j path specify objects search path\n"
1178 " -L path specify libraries search path\n"
1179 " -g file embed 'file' as a first header record with type 'generic'\n"
1180 " -mn name add module name record at the beginning of output file\n");
1184 int main(int argc
, char **argv
)
1186 char *outname
= "aout.rdf";
1187 int moduleloaded
= 0;
1188 char *respstrings
[128] = { 0, };
1192 options
.verbose
= 0;
1194 options
.dynalink
= 0;
1197 error_file
= stderr
;
1202 while (argc
&& *argv
&& **argv
== '-' && argv
[0][1] != 'l') {
1203 switch (argv
[0][1]) {
1205 printf("ldrdf (linker for RDF files) version " LDRDF_VERSION
1207 printf("RDOFF2 revision %s\n", RDOFF2_REVISION
);
1210 if (argv
[0][2] == '=') {
1211 options
.verbose
= argv
[0][3] - '0';
1212 if (options
.verbose
< 0 || options
.verbose
> 9) {
1214 "ldrdf: verbosity level must be a number"
1215 " between 0 and 9\n");
1222 options
.align
= atoi(argv
[1]);
1223 if (options
.align
<= 0) {
1225 "ldrdf: -a expects a positive number argument\n");
1234 if (argv
[0][2] == 'y')
1235 options
.dynalink
= 1;
1238 if (argv
[0][2] == 'n') {
1239 modname_specified
= argv
[1];
1242 fprintf(stderr
, "ldrdf: -mn expects a module name\n");
1253 options
.objpath
= 1;
1259 "ldrdf: more than one objects search path specified\n");
1264 options
.libpath
= 1;
1270 "ldrdf: more than one libraries search path specified\n");
1278 options
.respfile
= 1;
1279 if (argv
[1] != NULL
)
1280 f
= fopen(argv
[1], "r");
1283 "ldrdf: no response file name specified\n");
1289 "ldrdf: unable to open response file\n");
1294 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1298 if ((p
= strchr(buf
, '\n')) != NULL
)
1302 fprintf(stderr
, "ldrdf: too many input files\n");
1305 *(respstrings
+ i
) = nasm_strdup(buf
);
1312 options
.stderr_redir
= 1;
1313 error_file
= stdout
;
1316 generic_rec_file
= argv
[1];
1319 fprintf(stderr
, "ldrdf: -g expects a file name\n");
1329 if (options
.verbose
> 4) {
1330 printf("ldrdf invoked with options:\n");
1331 printf(" section alignment: %d bytes\n", options
.align
);
1332 printf(" output name: `%s'\n", outname
);
1334 printf(" strip symbols\n");
1335 if (options
.dynalink
)
1336 printf(" Unix-style dynamic linking\n");
1337 if (options
.objpath
)
1338 printf(" objects search path: %s\n", objpath
);
1339 if (options
.libpath
)
1340 printf(" libraries search path: %s\n", libpath
);
1344 symtab
= symtabNew();
1348 fprintf(stderr
, "ldrdf: out of memory\n");
1357 if (!strncmp(*argv
, "-l", 2)) {
1358 if (libpath
&& (argv
[0][2] != '/'))
1359 add_library(nasm_strcat(libpath
, *argv
+ 2));
1361 add_library(*argv
+ 2);
1363 if (objpath
&& (argv
[0][0] != '/'))
1364 loadmodule(nasm_strcat(objpath
, *argv
));
1372 if (!moduleloaded
) {
1373 printf("ldrdf: nothing to do. ldrdf -h for usage\n");
1379 if (options
.verbose
> 2) {
1380 printf("symbol table:\n");
1381 symtabDump(symtab
, stdout
);
1384 write_output(outname
);
1386 if (errorcount
> 0) {