1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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.
68 #define LDRDF_VERSION "1.07"
70 /* #define STINGY_MEMORY */
72 /* =======================================================================
73 * Types & macros that are private to this program
76 struct segment_infonode
{
77 int dest_seg
; /* output segment to be placed into, -1 to
78 skip linking this segment */
79 int32_t reloc
; /* segment's relocation factor */
83 rdffile f
; /* the RDOFF file structure */
84 struct segment_infonode seginfo
[RDF_MAXSEGS
]; /* what are we doing
88 struct modulenode
*next
;
94 #define newstr(str) strcpy(malloc(strlen(str) + 1),str)
95 #define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1)+strlen(s2)+1),s1),s2)
97 /* ==========================================================================
98 * Function prototypes of private utility functions
101 void processmodule(const char *filename
, struct modulenode
*mod
);
102 int allocnewseg(uint16_t type
, uint16_t reserved
);
103 int findsegment(uint16_t type
, uint16_t reserved
);
104 void symtab_add(const char *symbol
, int segment
, int32_t offset
);
105 int symtab_get(const char *symbol
, int *segment
, int32_t *offset
);
107 /* =========================================================================
108 * Global data structures.
111 /* a linked list of modules that will be included in the output */
112 struct modulenode
*modules
= NULL
;
113 struct modulenode
*lastmodule
= NULL
;
115 /* a linked list of libraries to be searched for unresolved imported symbols */
116 struct librarynode
*libraries
= NULL
;
117 struct librarynode
*lastlib
= NULL
;
119 /* the symbol table */
122 /* objects search path */
123 char *objpath
= NULL
;
125 /* libraries search path */
126 char *libpath
= NULL
;
128 /* file to embed as a generic record */
129 char *generic_rec_file
= NULL
;
132 static FILE *error_file
;
134 /* the header of the output file, built up stage by stage */
135 rdf_headerbuf
*newheader
= NULL
;
137 /* The current state of segment allocation, including information about
138 * which output segment numbers have been allocated, and their types and
139 * amount of data which has already been allocated inside them.
141 struct SegmentHeaderRec outputseg
[RDF_MAXSEGS
];
145 /* global options which affect how the program behaves */
146 struct ldrdfoptions
{
157 int errorcount
= 0; /* determines main program exit status */
159 /* =========================================================================
166 * sets up segments 0, 1, and 2, the initial code data and bss segments
171 outputseg
[0].type
= 1;
172 outputseg
[0].number
= 0;
173 outputseg
[0].reserved
= 0;
174 outputseg
[0].length
= 0;
175 outputseg
[1].type
= 2;
176 outputseg
[1].number
= 1;
177 outputseg
[1].reserved
= 0;
178 outputseg
[1].length
= 0;
179 outputseg
[2].type
= 0xFFFF; /* reserved segment type */
180 outputseg
[2].number
= 2;
181 outputseg
[2].reserved
= 0;
182 outputseg
[2].length
= 0;
189 * Determine the characteristics of a module, and decide what to do with
190 * each segment it contains (including determining destination segments and
191 * relocation factors for segments that are kept).
193 void loadmodule(const char *filename
)
196 printf("loading `%s'\n", filename
);
198 /* allocate a new module entry on the end of the modules list */
200 modules
= malloc(sizeof(*modules
));
201 lastmodule
= modules
;
203 lastmodule
->next
= malloc(sizeof(*modules
));
204 lastmodule
= lastmodule
->next
;
208 fprintf(stderr
, "ldrdf: out of memory\n");
212 /* open the file using 'rdfopen', which returns nonzero on error */
213 if (rdfopen(&lastmodule
->f
, filename
) != 0) {
214 rdfperror("ldrdf", filename
);
219 * store information about the module, and determine what segments
220 * it contains, and what we should do with them (determine relocation
221 * factor if we decide to keep them)
223 lastmodule
->header
= NULL
;
224 lastmodule
->name
= strdup(filename
);
225 lastmodule
->next
= NULL
;
227 processmodule(filename
, lastmodule
);
233 * step through each segment, determine what exactly we're doing with
234 * it, and if we intend to keep it, determine (a) which segment to
235 * put it in and (b) whereabouts in that segment it will end up.
236 * (b) is fairly easy, because we're now keeping track of how big each
237 * segment in our output file is...
239 void processmodule(const char *filename
, struct modulenode
*mod
)
241 struct segconfig sconf
;
245 int32_t bssamount
= 0;
246 int bss_was_referenced
= 0;
248 memset(&sconf
, 0, sizeof sconf
);
250 for (seg
= 0; seg
< mod
->f
.nsegs
; seg
++) {
252 * get the segment configuration for this type from the segment
253 * table. getsegconfig() is a macro, defined in ldsegs.h.
255 getsegconfig(sconf
, mod
->f
.seg
[seg
].type
);
257 if (options
.verbose
> 1) {
258 printf("%s %04x [%04x:%10s] ", filename
,
259 mod
->f
.seg
[seg
].number
, mod
->f
.seg
[seg
].type
,
263 * sconf->dowhat tells us what to do with a segment of this type.
265 switch (sconf
.dowhat
) {
268 * Set destination segment to -1, to indicate that this segment
269 * should be ignored for the purpose of output, ie it is left
270 * out of the linked executable.
272 mod
->seginfo
[seg
].dest_seg
= -1;
273 if (options
.verbose
> 1)
279 * The configuration tells us to create a new segment for
280 * each occurrence of this segment type.
282 outseg
= allocnewseg(sconf
.mergetype
,
283 mod
->f
.seg
[seg
].reserved
);
284 mod
->seginfo
[seg
].dest_seg
= outseg
;
285 mod
->seginfo
[seg
].reloc
= 0;
286 outputseg
[outseg
].length
= mod
->f
.seg
[seg
].length
;
287 if (options
.verbose
> 1)
288 printf("=> %04x:%08"PRIx32
" (+%04"PRIx32
")\n", outseg
,
289 mod
->seginfo
[seg
].reloc
, mod
->f
.seg
[seg
].length
);
294 * The configuration tells us to merge the segment with
295 * a previously existing segment of type 'sconf.mergetype',
296 * if one exists. Otherwise a new segment is created.
297 * This is handled transparently by 'findsegment()'.
299 outseg
= findsegment(sconf
.mergetype
,
300 mod
->f
.seg
[seg
].reserved
);
301 mod
->seginfo
[seg
].dest_seg
= outseg
;
304 * We need to add alignment to these segments.
306 if (outputseg
[outseg
].length
% options
.align
!= 0)
307 outputseg
[outseg
].length
+=
309 (outputseg
[outseg
].length
% options
.align
);
311 mod
->seginfo
[seg
].reloc
= outputseg
[outseg
].length
;
312 outputseg
[outseg
].length
+= mod
->f
.seg
[seg
].length
;
314 if (options
.verbose
> 1)
315 printf("=> %04x:%08"PRIx32
" (+%04"PRIx32
")\n", outseg
,
316 mod
->seginfo
[seg
].reloc
, mod
->f
.seg
[seg
].length
);
322 * extract symbols from the header, and dump them into the
325 header
= malloc(mod
->f
.header_len
);
327 fprintf(stderr
, "ldrdf: not enough memory\n");
330 if (rdfloadseg(&mod
->f
, RDOFF_HEADER
, header
)) {
331 rdfperror("ldrdf", filename
);
335 while ((hr
= rdfgetheaderrec(&mod
->f
))) {
337 case RDFREC_IMPORT
: /* imported symbol */
338 case RDFREC_FARIMPORT
:
339 /* Define with seg = -1 */
340 symtab_add(hr
->i
.label
, -1, 0);
343 case RDFREC_GLOBAL
:{ /* exported symbol */
347 if (hr
->e
.segment
== 2) {
348 bss_was_referenced
= 1;
349 destreloc
= bss_length
;
350 if (destreloc
% options
.align
!= 0)
352 options
.align
- (destreloc
% options
.align
);
356 mod
->seginfo
[(int)hr
->e
.segment
].dest_seg
) == -1)
358 destreloc
= mod
->seginfo
[(int)hr
->e
.segment
].reloc
;
360 symtab_add(hr
->e
.label
, destseg
, destreloc
+ hr
->e
.offset
);
364 case RDFREC_BSS
: /* BSS reservation */
366 * first, amalgamate all BSS reservations in this module
367 * into one, because we allow this in the output format.
369 bssamount
+= hr
->b
.amount
;
372 case RDFREC_COMMON
:{ /* Common variable */
373 symtabEnt
*ste
= symtabFind(symtab
, hr
->c
.label
);
375 /* Is the symbol already in the table? */
379 /* Align the variable */
380 if (bss_length
% hr
->c
.align
!= 0)
381 bss_length
+= hr
->c
.align
- (bss_length
% hr
->c
.align
);
382 if (options
.verbose
> 1) {
383 printf("%s %04x common '%s' => 0002:%08"PRIx32
" (+%04"PRIx32
")\n",
384 filename
, hr
->c
.segment
, hr
->c
.label
,
385 bss_length
, hr
->c
.size
);
388 symtab_add(hr
->c
.label
, 2, bss_length
);
389 mod
->bss_reloc
= bss_length
;
390 bss_length
+= hr
->c
.size
;
396 if (bssamount
!= 0 || bss_was_referenced
) {
398 * handle the BSS segment - first pad the existing bss length
399 * to the correct alignment, then store the length in bss_reloc
400 * for this module. Then add this module's BSS length onto
403 if (bss_length
% options
.align
!= 0)
404 bss_length
+= options
.align
- (bss_length
% options
.align
);
406 mod
->bss_reloc
= bss_length
;
407 if (options
.verbose
> 1) {
408 printf("%s 0002 [ BSS] => 0002:%08"PRIx32
" (+%04"PRIx32
")\n",
409 filename
, bss_length
, bssamount
);
411 bss_length
+= bssamount
;
415 * we free the header buffer here, to save memory later.
416 * this isn't efficient, but probably halves the memory usage
419 mod
->f
.header_loc
= NULL
;
427 * Return 1 if a given module is in the list, 0 otherwise.
429 int lookformodule(const char *name
)
431 struct modulenode
*curr
= modules
;
434 if (!strcmp(name
, curr
->name
))
445 * These functions manipulate the array of output segments, and are used
446 * by processmodule(). allocnewseg() allocates a segment in the array,
447 * initialising it to be empty. findsegment() first scans the array for
448 * a segment of the type requested, and if one isn't found allocates a
451 int allocnewseg(uint16_t type
, uint16_t reserved
)
453 outputseg
[nsegs
].type
= type
;
454 outputseg
[nsegs
].number
= nsegs
;
455 outputseg
[nsegs
].reserved
= reserved
;
456 outputseg
[nsegs
].length
= 0;
457 outputseg
[nsegs
].offset
= 0;
458 outputseg
[nsegs
].data
= NULL
;
463 int findsegment(uint16_t type
, uint16_t reserved
)
467 for (i
= 0; i
< nsegs
; i
++)
468 if (outputseg
[i
].type
== type
)
471 return allocnewseg(type
, reserved
);
477 * inserts a symbol into the global symbol table, which associates symbol
478 * names either with addresses, or a marker that the symbol hasn't been
479 * resolved yet, or possibly that the symbol has been defined as
480 * contained in a dynamic [load time/run time] linked library.
482 * segment = -1 => not yet defined
483 * segment = -2 => defined as dll symbol
485 * If the symbol is already defined, and the new segment >= 0, then
486 * if the original segment was < 0 the symbol is redefined, otherwise
487 * a duplicate symbol warning is issued. If new segment == -1, this
488 * routine won't change a previously existing symbol. It will change
489 * to segment = -2 only if the segment was previously < 0.
491 void symtab_add(const char *symbol
, int segment
, int32_t offset
)
495 ste
= symtabFind(symtab
, symbol
);
497 if (ste
->segment
>= 0) {
499 * symbol previously defined
503 fprintf(error_file
, "warning: `%s' redefined\n", symbol
);
508 * somebody wanted the symbol, and put an undefined symbol
509 * marker into the table
514 * we have more information now - update the symbol's entry
516 ste
->segment
= segment
;
517 ste
->offset
= offset
;
522 * this is the first declaration of this symbol
524 ste
= malloc(sizeof(symtabEnt
));
526 fprintf(stderr
, "ldrdf: out of memory\n");
529 ste
->name
= strdup(symbol
);
530 ste
->segment
= segment
;
531 ste
->offset
= offset
;
533 symtabInsert(symtab
, ste
);
539 * Retrieves the values associated with a symbol. Undefined symbols
540 * are assumed to have -1:0 associated. Returns 1 if the symbol was
541 * successfully located.
543 int symtab_get(const char *symbol
, int *segment
, int32_t *offset
)
545 symtabEnt
*ste
= symtabFind(symtab
, symbol
);
551 *segment
= ste
->segment
;
552 *offset
= ste
->offset
;
560 * checks that a library can be opened and is in the correct format,
561 * then adds it to the linked list of libraries.
563 void add_library(const char *name
)
565 if (rdl_verify(name
)) {
566 rdl_perror("ldrdf", name
);
571 lastlib
= libraries
= malloc(sizeof(*libraries
));
573 fprintf(stderr
, "ldrdf: out of memory\n");
577 lastlib
->next
= malloc(sizeof(*libraries
));
578 if (!lastlib
->next
) {
579 fprintf(stderr
, "ldrdf: out of memory\n");
582 lastlib
= lastlib
->next
;
584 lastlib
->next
= NULL
;
585 if (rdl_open(lastlib
, name
)) {
586 rdl_perror("ldrdf", name
);
595 * scans through the list of libraries, attempting to match symbols
596 * defined in library modules against symbols that are referenced but
597 * not defined (segment = -1 in the symbol table)
599 * returns 1 if any extra library modules are included, indicating that
600 * another pass through the library list should be made (possibly).
602 int search_libraries()
604 struct librarynode
*cur
;
610 int doneanything
= 0, pass
= 1, keepfile
;
616 if (options
.verbose
> 2)
617 printf("scanning library `%s', pass %d...\n", cur
->name
, pass
);
619 for (i
= 0; rdl_openmodule(cur
, i
, &f
) == 0; i
++) {
620 if (pass
== 2 && lookformodule(f
.name
))
623 if (options
.verbose
> 3)
624 printf(" looking in module `%s'\n", f
.name
);
626 header
= malloc(f
.header_len
);
628 fprintf(stderr
, "ldrdf: not enough memory\n");
631 if (rdfloadseg(&f
, RDOFF_HEADER
, header
)) {
632 rdfperror("ldrdf", f
.name
);
639 while ((hr
= rdfgetheaderrec(&f
))) {
640 /* We're only interested in exports, so skip others */
641 if (hr
->type
!= RDFREC_GLOBAL
)
645 * If the symbol is marked as SYM_GLOBAL, somebody will be
646 * definitely interested in it..
648 if ((hr
->e
.flags
& SYM_GLOBAL
) == 0) {
650 * otherwise the symbol is just public. Find it in
651 * the symbol table. If the symbol isn't defined, we
652 * aren't interested, so go on to the next.
653 * If it is defined as anything but -1, we're also not
654 * interested. But if it is defined as -1, insert this
655 * module into the list of modules to use, and go
656 * immediately on to the next module...
658 if (!symtab_get(hr
->e
.label
, &segment
, &offset
)
667 * as there are undefined symbols, we can assume that
668 * there are modules on the module list by the time
671 lastmodule
->next
= malloc(sizeof(*lastmodule
->next
));
672 if (!lastmodule
->next
) {
673 fprintf(stderr
, "ldrdf: not enough memory\n");
676 lastmodule
= lastmodule
->next
;
677 memcpy(&lastmodule
->f
, &f
, sizeof(f
));
678 lastmodule
->name
= strdup(f
.name
);
679 lastmodule
->next
= NULL
;
680 processmodule(f
.name
, lastmodule
);
689 if (rdl_error
!= 0 && rdl_error
!= RDL_ENOTFOUND
)
690 rdl_perror("ldrdf", cur
->name
);
693 if (cur
== NULL
&& pass
== 1) {
705 * this takes the linked list of modules, and walks through it, merging
706 * all the modules into a single output module, and then writes this to a
709 void write_output(const char *filename
)
712 rdf_headerbuf
*rdfheader
;
713 struct modulenode
*cur
;
714 int i
, availableseg
, seg
, localseg
, isrelative
;
716 rdfheaderrec
*hr
, newrec
;
722 if ((f
= fopen(filename
, "wb")) == NULL
) {
723 fprintf(stderr
, "ldrdf: couldn't open %s for output\n", filename
);
726 if ((rdfheader
= rdfnewheader()) == NULL
) {
727 fprintf(stderr
, "ldrdf: out of memory\n");
732 * If '-g' option was given, first record in output file will be a
733 * `generic' record, filled with a given file content.
734 * This can be useful, for example, when constructing multiboot
737 if (generic_rec_file
) {
741 printf("\nadding generic record from binary file %s\n",
744 hr
= (rdfheaderrec
*) malloc(sizeof(struct GenericRec
));
745 if ((ff
= fopen(generic_rec_file
, "r")) == NULL
) {
746 fprintf(stderr
, "ldrdf: couldn't open %s for input\n",
750 i
= fread(hr
->g
.data
, 1, sizeof(hr
->g
.data
), ff
);
751 fseek(ff
, 0, SEEK_END
);
752 if (ftell(ff
) > (long)sizeof(hr
->g
.data
)) {
754 "warning: maximum generic record size is %u, "
755 "rest of file ignored\n",
756 (unsigned int)sizeof(hr
->g
.data
));
762 rdfaddheader(rdfheader
, hr
);
767 printf("\nbuilding output module (%d segments)\n", nsegs
);
770 * Allocate the memory for the segments. We may be better off
771 * building the output module one segment at a time when running
772 * under 16 bit DOS, but that would be a slower way of doing this.
773 * And you could always use DJGPP...
775 for (i
= 0; i
< nsegs
; i
++) {
776 outputseg
[i
].data
= NULL
;
777 if (!outputseg
[i
].length
)
779 outputseg
[i
].data
= malloc(outputseg
[i
].length
);
780 if (!outputseg
[i
].data
) {
781 fprintf(stderr
, "ldrdf: out of memory\n");
787 * initialise availableseg, used to allocate segment numbers for
788 * imported and exported labels...
790 availableseg
= nsegs
;
793 * Step through the modules, performing required actions on each one
795 for (cur
= modules
; cur
; cur
= cur
->next
) {
797 * Read the actual segment contents into the correct places in
798 * the newly allocated segments
801 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
802 int dest
= cur
->seginfo
[i
].dest_seg
;
806 if (rdfloadseg(&cur
->f
, i
,
807 outputseg
[dest
].data
+ cur
->seginfo
[i
].reloc
)) {
808 rdfperror("ldrdf", cur
->name
);
814 * Perform fixups, and add new header records where required
817 header
= malloc(cur
->f
.header_len
);
819 fprintf(stderr
, "ldrdf: out of memory\n");
823 if (cur
->f
.header_loc
)
824 rdfheaderrewind(&cur
->f
);
825 else if (rdfloadseg(&cur
->f
, RDOFF_HEADER
, header
)) {
826 rdfperror("ldrdf", cur
->name
);
831 * we need to create a local segment number -> location
832 * table for the segments in this module.
834 init_seglocations(&segs
);
835 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
836 add_seglocation(&segs
, cur
->f
.seg
[i
].number
,
837 cur
->seginfo
[i
].dest_seg
,
838 cur
->seginfo
[i
].reloc
);
841 * and the BSS segment (doh!)
843 add_seglocation(&segs
, 2, 2, cur
->bss_reloc
);
845 while ((hr
= rdfgetheaderrec(&cur
->f
))) {
847 case RDFREC_RELOC
: /* relocation record - need to do a fixup */
849 * First correct the offset stored in the segment from
850 * the start of the segment (which may well have changed).
852 * To do this we add to the number stored the relocation
853 * factor associated with the segment that contains the
856 * The relocation could be a relative relocation, in which
857 * case we have to first subtract the amount we've relocated
858 * the containing segment by.
860 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
862 "%s: reloc to undefined segment %04x\n",
863 cur
->name
, (int)hr
->r
.refseg
);
869 (hr
->r
.segment
& RDOFF_RELATIVEMASK
) ==
871 hr
->r
.segment
&= (RDOFF_RELATIVEMASK
- 1);
873 if (hr
->r
.segment
== 2 ||
875 rdffindsegment(&cur
->f
, hr
->r
.segment
)) == -1) {
876 fprintf(stderr
, "%s: reloc from %s segment (%d)\n",
878 hr
->r
.segment
== 2 ? "BSS" : "unknown",
884 if (hr
->r
.length
!= 1 && hr
->r
.length
!= 2 &&
886 fprintf(stderr
, "%s: nonstandard length reloc "
887 "(%d bytes)\n", cur
->name
, hr
->r
.length
);
893 * okay, now the relocation is in the segment pointed to by
894 * cur->seginfo[localseg], and we know everything else is
895 * okay to go ahead and do the relocation
897 data
= outputseg
[cur
->seginfo
[localseg
].dest_seg
].data
;
898 data
+= cur
->seginfo
[localseg
].reloc
+ hr
->r
.offset
;
901 * data now points to the reference that needs
902 * relocation. Calculate the relocation factor.
904 * offset of referred object in segment [in offset]
905 * (- relocation of localseg, if ref is relative)
906 * For simplicity, the result is stored in 'offset'.
907 * Then add 'offset' onto the value at data.
911 offset
-= cur
->seginfo
[localseg
].reloc
;
912 switch (hr
->r
.length
) {
915 if (offset
< -127 || offset
> 128)
917 "warning: relocation out of range "
918 "at %s(%02x:%08"PRIx32
")\n", cur
->name
,
919 (int)hr
->r
.segment
, hr
->r
.offset
);
920 *data
= (char)offset
;
923 offset
+= *(int16_t *)data
;
924 if (offset
< -32767 || offset
> 32768)
926 "warning: relocation out of range "
927 "at %s(%02x:%08"PRIx32
")\n", cur
->name
,
928 (int)hr
->r
.segment
, hr
->r
.offset
);
929 *(int16_t *)data
= (int16_t)offset
;
932 *(int32_t *)data
+= offset
;
933 /* we can't easily detect overflow on this one */
938 * If the relocation was relative between two symbols in
939 * the same segment, then we're done.
941 * Otherwise, we need to output a new relocation record
942 * with the references updated segment and offset...
944 if (!isrelative
|| cur
->seginfo
[localseg
].dest_seg
!= seg
) {
945 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
946 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
949 hr
->r
.segment
+= RDOFF_RELATIVEMASK
;
950 rdfaddheader(rdfheader
, hr
);
954 case RDFREC_IMPORT
: /* import symbol */
955 case RDFREC_FARIMPORT
:
957 * scan the global symbol table for the symbol
958 * and associate its location with the segment number
961 se
= symtabFind(symtab
, hr
->i
.label
);
962 if (!se
|| se
->segment
== -1) {
963 if (!options
.dynalink
&& !(hr
->i
.flags
& SYM_IMPORT
)) {
965 "error: unresolved reference to `%s'"
966 " in module `%s'\n", hr
->i
.label
,
971 * we need to allocate a segment number for this
972 * symbol, and store it in the symbol table for
976 se
= malloc(sizeof(*se
));
978 fprintf(stderr
, "ldrdf: out of memory\n");
981 se
->name
= strdup(hr
->i
.label
);
983 se
->segment
= availableseg
++;
985 symtabInsert(symtab
, se
);
987 se
->segment
= availableseg
++;
991 * output a header record that imports it to the
992 * recently allocated segment number...
995 newrec
.i
.segment
= se
->segment
;
996 rdfaddheader(rdfheader
, &newrec
);
999 add_seglocation(&segs
, hr
->i
.segment
, se
->segment
,
1003 case RDFREC_GLOBAL
: /* export symbol */
1005 * need to insert an export for this symbol into the new
1006 * header, unless we're stripping symbols. Even if we're
1007 * stripping, put the symbol if it's marked as SYM_GLOBAL.
1009 if (options
.strip
&& !(hr
->e
.flags
& SYM_GLOBAL
))
1012 if (hr
->e
.segment
== 2) {
1014 offset
= cur
->bss_reloc
;
1016 localseg
= rdffindsegment(&cur
->f
, hr
->e
.segment
);
1017 if (localseg
== -1) {
1018 fprintf(stderr
, "%s: exported symbol `%s' from "
1019 "unrecognised segment\n", cur
->name
,
1024 offset
= cur
->seginfo
[localseg
].reloc
;
1025 seg
= cur
->seginfo
[localseg
].dest_seg
;
1028 hr
->e
.segment
= seg
;
1029 hr
->e
.offset
+= offset
;
1030 rdfaddheader(rdfheader
, hr
);
1033 case RDFREC_MODNAME
: /* module name */
1035 * Insert module name record if export symbols
1037 * If module name begins with '$' - insert it anyway.
1039 if (options
.strip
&& hr
->m
.modname
[0] != '$')
1041 rdfaddheader(rdfheader
, hr
);
1044 case RDFREC_DLL
: /* DLL name */
1046 * Insert DLL name if it begins with '$'
1048 if (hr
->d
.libname
[0] != '$')
1050 rdfaddheader(rdfheader
, hr
);
1053 case RDFREC_SEGRELOC
: /* segment fixup */
1055 * modify the segment numbers if necessary, and
1056 * pass straight through to the output module header
1060 if (hr
->r
.segment
== 2) {
1061 fprintf(stderr
, "%s: segment fixup in BSS section\n",
1066 localseg
= rdffindsegment(&cur
->f
, hr
->r
.segment
);
1067 if (localseg
== -1) {
1068 fprintf(stderr
, "%s: segment fixup in unrecognised"
1069 " segment (%d)\n", cur
->name
, hr
->r
.segment
);
1073 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
1074 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
1076 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
1077 fprintf(stderr
, "%s: segment fixup to undefined "
1078 "segment %04x\n", cur
->name
,
1084 rdfaddheader(rdfheader
, hr
);
1087 case RDFREC_COMMON
: /* Common variable */
1088 /* Is this symbol already in the table? */
1089 se
= symtabFind(symtab
, hr
->c
.label
);
1091 printf("%s is not in symtab yet\n", hr
->c
.label
);
1094 /* Add segment location */
1095 add_seglocation(&segs
, hr
->c
.segment
, se
->segment
,
1102 done_seglocations(&segs
);
1107 * combined BSS reservation for the entire results
1109 newrec
.type
= RDFREC_BSS
;
1110 newrec
.b
.reclen
= 4;
1111 newrec
.b
.amount
= bss_length
;
1112 rdfaddheader(rdfheader
, &newrec
);
1117 for (i
= 0; i
< nsegs
; i
++) {
1120 rdfaddsegment(rdfheader
, outputseg
[i
].length
);
1123 rdfwriteheader(f
, rdfheader
);
1124 rdfdoneheader(rdfheader
);
1127 * Step through the segments, one at a time, writing out into
1130 for (i
= 0; i
< nsegs
; i
++) {
1137 s
= translateint16_t(outputseg
[i
].type
);
1138 fwrite(&s
, 2, 1, f
);
1139 s
= translateint16_t(outputseg
[i
].number
);
1140 fwrite(&s
, 2, 1, f
);
1141 s
= translateint16_t(outputseg
[i
].reserved
);
1142 fwrite(&s
, 2, 1, f
);
1143 l
= translateint32_t(outputseg
[i
].length
);
1144 fwrite(&l
, 4, 1, f
);
1146 fwrite(outputseg
[i
].data
, outputseg
[i
].length
, 1, f
);
1152 /* =========================================================================
1159 " ldrdf [options] object modules ... [-llibrary ...]\n"
1162 " -v[=n] increase verbosity by 1, or set it to n\n"
1163 " -a nn set segment alignment value (default 16)\n"
1164 " -s strip public symbols\n"
1165 " -dy Unix-style dynamic linking\n"
1166 " -o name write output in file 'name'\n"
1167 " -j path specify objects search path\n"
1168 " -L path specify libraries search path\n"
1169 " -g file embed 'file' as a first header record with type 'generic'\n");
1173 int main(int argc
, char **argv
)
1175 char *outname
= "aout.rdf";
1176 int moduleloaded
= 0;
1177 char *respstrings
[128] = { 0, };
1179 options
.verbose
= 0;
1181 options
.dynalink
= 0;
1184 error_file
= stderr
;
1189 while (argc
&& *argv
&& **argv
== '-' && argv
[0][1] != 'l') {
1190 switch (argv
[0][1]) {
1192 printf("ldrdf (linker for RDF files) version " LDRDF_VERSION
1194 printf("RDOFF2 revision %s\n", RDOFF2_REVISION
);
1197 if (argv
[0][2] == '=') {
1198 options
.verbose
= argv
[0][3] - '0';
1199 if (options
.verbose
< 0 || options
.verbose
> 9) {
1201 "ldrdf: verbosity level must be a number"
1202 " between 0 and 9\n");
1209 options
.align
= atoi(argv
[1]);
1210 if (options
.align
<= 0) {
1212 "ldrdf: -a expects a positive number argument\n");
1221 if (argv
[0][2] == 'y')
1222 options
.dynalink
= 1;
1230 options
.objpath
= 1;
1236 "ldrdf: more than one objects search path specified\n");
1241 options
.libpath
= 1;
1247 "ldrdf: more than one libraries search path specified\n");
1255 options
.respfile
= 1;
1256 if (argv
[1] != NULL
)
1257 f
= fopen(argv
[1], "r");
1260 "ldrdf: no response file name specified\n");
1266 "ldrdf: unable to open response file\n");
1271 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1275 if ((p
= strchr(buf
, '\n')) != NULL
)
1279 fprintf(stderr
, "ldrdf: too many input files\n");
1282 *(respstrings
+ i
) = newstr(buf
);
1289 options
.stderr_redir
= 1;
1290 error_file
= stdout
;
1293 generic_rec_file
= argv
[1];
1302 if (options
.verbose
> 4) {
1303 printf("ldrdf invoked with options:\n");
1304 printf(" section alignment: %d bytes\n", options
.align
);
1305 printf(" output name: `%s'\n", outname
);
1307 printf(" strip symbols\n");
1308 if (options
.dynalink
)
1309 printf(" Unix-style dynamic linking\n");
1310 if (options
.objpath
)
1311 printf(" objects search path: %s\n", objpath
);
1312 if (options
.libpath
)
1313 printf(" libraries search path: %s\n", libpath
);
1317 symtab
= symtabNew();
1321 fprintf(stderr
, "ldrdf: out of memory\n");
1330 if (!strncmp(*argv
, "-l", 2)) {
1331 if (libpath
&& (argv
[0][2] != '/'))
1332 add_library(newstrcat(libpath
, *argv
+ 2));
1334 add_library(*argv
+ 2);
1336 if (objpath
&& (argv
[0][0] != '/'))
1337 loadmodule(newstrcat(objpath
, *argv
));
1345 if (!moduleloaded
) {
1346 printf("ldrdf: nothing to do. ldrdf -h for usage\n");
1352 if (options
.verbose
> 2) {
1353 printf("symbol table:\n");
1354 symtabDump(symtab
, stdout
);
1357 write_output(outname
);