2 * ldrdf.c - RDOFF Object File linker/loader main program.
4 * Copyright (c) 1996,99 Julian Hall. All rights reserved.
5 * Improvements and fixes (c) 1999-2004 RET & COM Research.
7 * This file is distributed under the terms and conditions of the
8 * GNU Lesser Public License (LGPL), version 2.1.
9 * See http://www.gnu.org/copyleft/lgpl.html for details.
14 * - enhance search of required export symbols in libraries (now depends
15 * on modules order in library)
16 * - keep a cache of symbol names in each library module so
17 * we don't have to constantly recheck the file
18 * - general performance improvements
20 * BUGS & LIMITATIONS: this program doesn't support multiple code, data
21 * or bss segments, therefore for 16 bit programs whose code, data or BSS
22 * segment exceeds 64K in size, it will not work. This program probably
23 * won't work if compiled by a 16 bit compiler. Try DJGPP if you're running
24 * under DOS. '#define STINGY_MEMORY' may help a little.
41 #define LDRDF_VERSION "1.07"
43 /* #define STINGY_MEMORY */
45 /* =======================================================================
46 * Types & macros that are private to this program
49 struct segment_infonode
{
50 int dest_seg
; /* output segment to be placed into, -1 to
51 skip linking this segment */
52 int32_t reloc
; /* segment's relocation factor */
56 rdffile f
; /* the RDOFF file structure */
57 struct segment_infonode seginfo
[RDF_MAXSEGS
]; /* what are we doing
61 struct modulenode
*next
;
67 #define newstr(str) strcpy(malloc(strlen(str) + 1),str)
68 #define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1)+strlen(s2)+1),s1),s2)
70 /* ==========================================================================
71 * Function prototypes of private utility functions
74 void processmodule(const char *filename
, struct modulenode
*mod
);
75 int allocnewseg(uint16_t type
, uint16_t reserved
);
76 int findsegment(uint16_t type
, uint16_t reserved
);
77 void symtab_add(const char *symbol
, int segment
, int32_t offset
);
78 int symtab_get(const char *symbol
, int *segment
, int32_t *offset
);
80 /* =========================================================================
81 * Global data structures.
84 /* a linked list of modules that will be included in the output */
85 struct modulenode
*modules
= NULL
;
86 struct modulenode
*lastmodule
= NULL
;
88 /* a linked list of libraries to be searched for unresolved imported symbols */
89 struct librarynode
*libraries
= NULL
;
90 struct librarynode
*lastlib
= NULL
;
92 /* the symbol table */
95 /* objects search path */
98 /* libraries search path */
101 /* file to embed as a generic record */
102 char *generic_rec_file
= NULL
;
105 static FILE *error_file
;
107 /* the header of the output file, built up stage by stage */
108 rdf_headerbuf
*newheader
= NULL
;
110 /* The current state of segment allocation, including information about
111 * which output segment numbers have been allocated, and their types and
112 * amount of data which has already been allocated inside them.
114 struct SegmentHeaderRec outputseg
[RDF_MAXSEGS
];
118 /* global options which affect how the program behaves */
119 struct ldrdfoptions
{
130 int errorcount
= 0; /* determines main program exit status */
132 /* =========================================================================
139 * sets up segments 0, 1, and 2, the initial code data and bss segments
144 outputseg
[0].type
= 1;
145 outputseg
[0].number
= 0;
146 outputseg
[0].reserved
= 0;
147 outputseg
[0].length
= 0;
148 outputseg
[1].type
= 2;
149 outputseg
[1].number
= 1;
150 outputseg
[1].reserved
= 0;
151 outputseg
[1].length
= 0;
152 outputseg
[2].type
= 0xFFFF; /* reserved segment type */
153 outputseg
[2].number
= 2;
154 outputseg
[2].reserved
= 0;
155 outputseg
[2].length
= 0;
162 * Determine the characteristics of a module, and decide what to do with
163 * each segment it contains (including determining destination segments and
164 * relocation factors for segments that are kept).
166 void loadmodule(const char *filename
)
169 printf("loading `%s'\n", filename
);
171 /* allocate a new module entry on the end of the modules list */
173 modules
= malloc(sizeof(*modules
));
174 lastmodule
= modules
;
176 lastmodule
->next
= malloc(sizeof(*modules
));
177 lastmodule
= lastmodule
->next
;
181 fprintf(stderr
, "ldrdf: out of memory\n");
185 /* open the file using 'rdfopen', which returns nonzero on error */
186 if (rdfopen(&lastmodule
->f
, filename
) != 0) {
187 rdfperror("ldrdf", filename
);
192 * store information about the module, and determine what segments
193 * it contains, and what we should do with them (determine relocation
194 * factor if we decide to keep them)
196 lastmodule
->header
= NULL
;
197 lastmodule
->name
= strdup(filename
);
198 lastmodule
->next
= NULL
;
200 processmodule(filename
, lastmodule
);
206 * step through each segment, determine what exactly we're doing with
207 * it, and if we intend to keep it, determine (a) which segment to
208 * put it in and (b) whereabouts in that segment it will end up.
209 * (b) is fairly easy, because we're now keeping track of how big each
210 * segment in our output file is...
212 void processmodule(const char *filename
, struct modulenode
*mod
)
214 struct segconfig sconf
;
218 int32_t bssamount
= 0;
219 int bss_was_referenced
= 0;
221 memset(&sconf
, 0, sizeof sconf
);
223 for (seg
= 0; seg
< mod
->f
.nsegs
; seg
++) {
225 * get the segment configuration for this type from the segment
226 * table. getsegconfig() is a macro, defined in ldsegs.h.
228 getsegconfig(sconf
, mod
->f
.seg
[seg
].type
);
230 if (options
.verbose
> 1) {
231 printf("%s %04x [%04x:%10s] ", filename
,
232 mod
->f
.seg
[seg
].number
, mod
->f
.seg
[seg
].type
,
236 * sconf->dowhat tells us what to do with a segment of this type.
238 switch (sconf
.dowhat
) {
241 * Set destination segment to -1, to indicate that this segment
242 * should be ignored for the purpose of output, ie it is left
243 * out of the linked executable.
245 mod
->seginfo
[seg
].dest_seg
= -1;
246 if (options
.verbose
> 1)
252 * The configuration tells us to create a new segment for
253 * each occurrence of this segment type.
255 outseg
= allocnewseg(sconf
.mergetype
,
256 mod
->f
.seg
[seg
].reserved
);
257 mod
->seginfo
[seg
].dest_seg
= outseg
;
258 mod
->seginfo
[seg
].reloc
= 0;
259 outputseg
[outseg
].length
= mod
->f
.seg
[seg
].length
;
260 if (options
.verbose
> 1)
261 printf("=> %04x:%08"PRIx32
" (+%04"PRIx32
")\n", outseg
,
262 mod
->seginfo
[seg
].reloc
, mod
->f
.seg
[seg
].length
);
267 * The configuration tells us to merge the segment with
268 * a previously existing segment of type 'sconf.mergetype',
269 * if one exists. Otherwise a new segment is created.
270 * This is handled transparently by 'findsegment()'.
272 outseg
= findsegment(sconf
.mergetype
,
273 mod
->f
.seg
[seg
].reserved
);
274 mod
->seginfo
[seg
].dest_seg
= outseg
;
277 * We need to add alignment to these segments.
279 if (outputseg
[outseg
].length
% options
.align
!= 0)
280 outputseg
[outseg
].length
+=
282 (outputseg
[outseg
].length
% options
.align
);
284 mod
->seginfo
[seg
].reloc
= outputseg
[outseg
].length
;
285 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
);
295 * extract symbols from the header, and dump them into the
298 header
= malloc(mod
->f
.header_len
);
300 fprintf(stderr
, "ldrdf: not enough memory\n");
303 if (rdfloadseg(&mod
->f
, RDOFF_HEADER
, header
)) {
304 rdfperror("ldrdf", filename
);
308 while ((hr
= rdfgetheaderrec(&mod
->f
))) {
310 case RDFREC_IMPORT
: /* imported symbol */
311 case RDFREC_FARIMPORT
:
312 /* Define with seg = -1 */
313 symtab_add(hr
->i
.label
, -1, 0);
316 case RDFREC_GLOBAL
:{ /* exported symbol */
320 if (hr
->e
.segment
== 2) {
321 bss_was_referenced
= 1;
322 destreloc
= bss_length
;
323 if (destreloc
% options
.align
!= 0)
325 options
.align
- (destreloc
% options
.align
);
329 mod
->seginfo
[(int)hr
->e
.segment
].dest_seg
) == -1)
331 destreloc
= mod
->seginfo
[(int)hr
->e
.segment
].reloc
;
333 symtab_add(hr
->e
.label
, destseg
, destreloc
+ hr
->e
.offset
);
337 case RDFREC_BSS
: /* BSS reservation */
339 * first, amalgamate all BSS reservations in this module
340 * into one, because we allow this in the output format.
342 bssamount
+= hr
->b
.amount
;
345 case RDFREC_COMMON
:{ /* Common variable */
346 symtabEnt
*ste
= symtabFind(symtab
, hr
->c
.label
);
348 /* Is the symbol already in the table? */
352 /* Align the variable */
353 if (bss_length
% hr
->c
.align
!= 0)
354 bss_length
+= hr
->c
.align
- (bss_length
% hr
->c
.align
);
355 if (options
.verbose
> 1) {
356 printf("%s %04x common '%s' => 0002:%08"PRIx32
" (+%04"PRIx32
")\n",
357 filename
, hr
->c
.segment
, hr
->c
.label
,
358 bss_length
, hr
->c
.size
);
361 symtab_add(hr
->c
.label
, 2, bss_length
);
362 mod
->bss_reloc
= bss_length
;
363 bss_length
+= hr
->c
.size
;
369 if (bssamount
!= 0 || bss_was_referenced
) {
371 * handle the BSS segment - first pad the existing bss length
372 * to the correct alignment, then store the length in bss_reloc
373 * for this module. Then add this module's BSS length onto
376 if (bss_length
% options
.align
!= 0)
377 bss_length
+= options
.align
- (bss_length
% options
.align
);
379 mod
->bss_reloc
= bss_length
;
380 if (options
.verbose
> 1) {
381 printf("%s 0002 [ BSS] => 0002:%08"PRIx32
" (+%04"PRIx32
")\n",
382 filename
, bss_length
, bssamount
);
384 bss_length
+= bssamount
;
388 * we free the header buffer here, to save memory later.
389 * this isn't efficient, but probably halves the memory usage
392 mod
->f
.header_loc
= NULL
;
400 * Return 1 if a given module is in the list, 0 otherwise.
402 int lookformodule(const char *name
)
404 struct modulenode
*curr
= modules
;
407 if (!strcmp(name
, curr
->name
))
418 * These functions manipulate the array of output segments, and are used
419 * by processmodule(). allocnewseg() allocates a segment in the array,
420 * initialising it to be empty. findsegment() first scans the array for
421 * a segment of the type requested, and if one isn't found allocates a
424 int allocnewseg(uint16_t type
, uint16_t reserved
)
426 outputseg
[nsegs
].type
= type
;
427 outputseg
[nsegs
].number
= nsegs
;
428 outputseg
[nsegs
].reserved
= reserved
;
429 outputseg
[nsegs
].length
= 0;
430 outputseg
[nsegs
].offset
= 0;
431 outputseg
[nsegs
].data
= NULL
;
436 int findsegment(uint16_t type
, uint16_t reserved
)
440 for (i
= 0; i
< nsegs
; i
++)
441 if (outputseg
[i
].type
== type
)
444 return allocnewseg(type
, reserved
);
450 * inserts a symbol into the global symbol table, which associates symbol
451 * names either with addresses, or a marker that the symbol hasn't been
452 * resolved yet, or possibly that the symbol has been defined as
453 * contained in a dynamic [load time/run time] linked library.
455 * segment = -1 => not yet defined
456 * segment = -2 => defined as dll symbol
458 * If the symbol is already defined, and the new segment >= 0, then
459 * if the original segment was < 0 the symbol is redefined, otherwise
460 * a duplicate symbol warning is issued. If new segment == -1, this
461 * routine won't change a previously existing symbol. It will change
462 * to segment = -2 only if the segment was previously < 0.
464 void symtab_add(const char *symbol
, int segment
, int32_t offset
)
468 ste
= symtabFind(symtab
, symbol
);
470 if (ste
->segment
>= 0) {
472 * symbol previously defined
476 fprintf(error_file
, "warning: `%s' redefined\n", symbol
);
481 * somebody wanted the symbol, and put an undefined symbol
482 * marker into the table
487 * we have more information now - update the symbol's entry
489 ste
->segment
= segment
;
490 ste
->offset
= offset
;
495 * this is the first declaration of this symbol
497 ste
= malloc(sizeof(symtabEnt
));
499 fprintf(stderr
, "ldrdf: out of memory\n");
502 ste
->name
= strdup(symbol
);
503 ste
->segment
= segment
;
504 ste
->offset
= offset
;
506 symtabInsert(symtab
, ste
);
512 * Retrieves the values associated with a symbol. Undefined symbols
513 * are assumed to have -1:0 associated. Returns 1 if the symbol was
514 * successfully located.
516 int symtab_get(const char *symbol
, int *segment
, int32_t *offset
)
518 symtabEnt
*ste
= symtabFind(symtab
, symbol
);
524 *segment
= ste
->segment
;
525 *offset
= ste
->offset
;
533 * checks that a library can be opened and is in the correct format,
534 * then adds it to the linked list of libraries.
536 void add_library(const char *name
)
538 if (rdl_verify(name
)) {
539 rdl_perror("ldrdf", name
);
544 lastlib
= libraries
= malloc(sizeof(*libraries
));
546 fprintf(stderr
, "ldrdf: out of memory\n");
550 lastlib
->next
= malloc(sizeof(*libraries
));
551 if (!lastlib
->next
) {
552 fprintf(stderr
, "ldrdf: out of memory\n");
555 lastlib
= lastlib
->next
;
557 lastlib
->next
= NULL
;
558 if (rdl_open(lastlib
, name
)) {
559 rdl_perror("ldrdf", name
);
568 * scans through the list of libraries, attempting to match symbols
569 * defined in library modules against symbols that are referenced but
570 * not defined (segment = -1 in the symbol table)
572 * returns 1 if any extra library modules are included, indicating that
573 * another pass through the library list should be made (possibly).
575 int search_libraries()
577 struct librarynode
*cur
;
583 int doneanything
= 0, pass
= 1, keepfile
;
589 if (options
.verbose
> 2)
590 printf("scanning library `%s', pass %d...\n", cur
->name
, pass
);
592 for (i
= 0; rdl_openmodule(cur
, i
, &f
) == 0; i
++) {
593 if (pass
== 2 && lookformodule(f
.name
))
596 if (options
.verbose
> 3)
597 printf(" looking in module `%s'\n", f
.name
);
599 header
= malloc(f
.header_len
);
601 fprintf(stderr
, "ldrdf: not enough memory\n");
604 if (rdfloadseg(&f
, RDOFF_HEADER
, header
)) {
605 rdfperror("ldrdf", f
.name
);
612 while ((hr
= rdfgetheaderrec(&f
))) {
613 /* We're only interested in exports, so skip others */
614 if (hr
->type
!= RDFREC_GLOBAL
)
618 * If the symbol is marked as SYM_GLOBAL, somebody will be
619 * definitely interested in it..
621 if ((hr
->e
.flags
& SYM_GLOBAL
) == 0) {
623 * otherwise the symbol is just public. Find it in
624 * the symbol table. If the symbol isn't defined, we
625 * aren't interested, so go on to the next.
626 * If it is defined as anything but -1, we're also not
627 * interested. But if it is defined as -1, insert this
628 * module into the list of modules to use, and go
629 * immediately on to the next module...
631 if (!symtab_get(hr
->e
.label
, &segment
, &offset
)
640 * as there are undefined symbols, we can assume that
641 * there are modules on the module list by the time
644 lastmodule
->next
= malloc(sizeof(*lastmodule
->next
));
645 if (!lastmodule
->next
) {
646 fprintf(stderr
, "ldrdf: not enough memory\n");
649 lastmodule
= lastmodule
->next
;
650 memcpy(&lastmodule
->f
, &f
, sizeof(f
));
651 lastmodule
->name
= strdup(f
.name
);
652 lastmodule
->next
= NULL
;
653 processmodule(f
.name
, lastmodule
);
662 if (rdl_error
!= 0 && rdl_error
!= RDL_ENOTFOUND
)
663 rdl_perror("ldrdf", cur
->name
);
666 if (cur
== NULL
&& pass
== 1) {
678 * this takes the linked list of modules, and walks through it, merging
679 * all the modules into a single output module, and then writes this to a
682 void write_output(const char *filename
)
685 rdf_headerbuf
*rdfheader
;
686 struct modulenode
*cur
;
687 int i
, availableseg
, seg
, localseg
, isrelative
;
689 rdfheaderrec
*hr
, newrec
;
695 if ((f
= fopen(filename
, "wb")) == NULL
) {
696 fprintf(stderr
, "ldrdf: couldn't open %s for output\n", filename
);
699 if ((rdfheader
= rdfnewheader()) == NULL
) {
700 fprintf(stderr
, "ldrdf: out of memory\n");
705 * If '-g' option was given, first record in output file will be a
706 * `generic' record, filled with a given file content.
707 * This can be useful, for example, when constructing multiboot
710 if (generic_rec_file
) {
714 printf("\nadding generic record from binary file %s\n",
717 hr
= (rdfheaderrec
*) malloc(sizeof(struct GenericRec
));
718 if ((ff
= fopen(generic_rec_file
, "r")) == NULL
) {
719 fprintf(stderr
, "ldrdf: couldn't open %s for input\n",
723 i
= fread(hr
->g
.data
, 1, sizeof(hr
->g
.data
), ff
);
724 fseek(ff
, 0, SEEK_END
);
725 if (ftell(ff
) > (long)sizeof(hr
->g
.data
)) {
727 "warning: maximum generic record size is %u, "
728 "rest of file ignored\n",
729 (unsigned int)sizeof(hr
->g
.data
));
735 rdfaddheader(rdfheader
, hr
);
740 printf("\nbuilding output module (%d segments)\n", nsegs
);
743 * Allocate the memory for the segments. We may be better off
744 * building the output module one segment at a time when running
745 * under 16 bit DOS, but that would be a slower way of doing this.
746 * And you could always use DJGPP...
748 for (i
= 0; i
< nsegs
; i
++) {
749 outputseg
[i
].data
= NULL
;
750 if (!outputseg
[i
].length
)
752 outputseg
[i
].data
= malloc(outputseg
[i
].length
);
753 if (!outputseg
[i
].data
) {
754 fprintf(stderr
, "ldrdf: out of memory\n");
760 * initialise availableseg, used to allocate segment numbers for
761 * imported and exported labels...
763 availableseg
= nsegs
;
766 * Step through the modules, performing required actions on each one
768 for (cur
= modules
; cur
; cur
= cur
->next
) {
770 * Read the actual segment contents into the correct places in
771 * the newly allocated segments
774 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
775 int dest
= cur
->seginfo
[i
].dest_seg
;
779 if (rdfloadseg(&cur
->f
, i
,
780 outputseg
[dest
].data
+ cur
->seginfo
[i
].reloc
)) {
781 rdfperror("ldrdf", cur
->name
);
787 * Perform fixups, and add new header records where required
790 header
= malloc(cur
->f
.header_len
);
792 fprintf(stderr
, "ldrdf: out of memory\n");
796 if (cur
->f
.header_loc
)
797 rdfheaderrewind(&cur
->f
);
798 else if (rdfloadseg(&cur
->f
, RDOFF_HEADER
, header
)) {
799 rdfperror("ldrdf", cur
->name
);
804 * we need to create a local segment number -> location
805 * table for the segments in this module.
807 init_seglocations(&segs
);
808 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
809 add_seglocation(&segs
, cur
->f
.seg
[i
].number
,
810 cur
->seginfo
[i
].dest_seg
,
811 cur
->seginfo
[i
].reloc
);
814 * and the BSS segment (doh!)
816 add_seglocation(&segs
, 2, 2, cur
->bss_reloc
);
818 while ((hr
= rdfgetheaderrec(&cur
->f
))) {
820 case RDFREC_RELOC
: /* relocation record - need to do a fixup */
822 * First correct the offset stored in the segment from
823 * the start of the segment (which may well have changed).
825 * To do this we add to the number stored the relocation
826 * factor associated with the segment that contains the
829 * The relocation could be a relative relocation, in which
830 * case we have to first subtract the amount we've relocated
831 * the containing segment by.
833 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
835 "%s: reloc to undefined segment %04x\n",
836 cur
->name
, (int)hr
->r
.refseg
);
842 (hr
->r
.segment
& RDOFF_RELATIVEMASK
) ==
844 hr
->r
.segment
&= (RDOFF_RELATIVEMASK
- 1);
846 if (hr
->r
.segment
== 2 ||
848 rdffindsegment(&cur
->f
, hr
->r
.segment
)) == -1) {
849 fprintf(stderr
, "%s: reloc from %s segment (%d)\n",
851 hr
->r
.segment
== 2 ? "BSS" : "unknown",
857 if (hr
->r
.length
!= 1 && hr
->r
.length
!= 2 &&
859 fprintf(stderr
, "%s: nonstandard length reloc "
860 "(%d bytes)\n", cur
->name
, hr
->r
.length
);
866 * okay, now the relocation is in the segment pointed to by
867 * cur->seginfo[localseg], and we know everything else is
868 * okay to go ahead and do the relocation
870 data
= outputseg
[cur
->seginfo
[localseg
].dest_seg
].data
;
871 data
+= cur
->seginfo
[localseg
].reloc
+ hr
->r
.offset
;
874 * data now points to the reference that needs
875 * relocation. Calculate the relocation factor.
877 * offset of referred object in segment [in offset]
878 * (- relocation of localseg, if ref is relative)
879 * For simplicity, the result is stored in 'offset'.
880 * Then add 'offset' onto the value at data.
884 offset
-= cur
->seginfo
[localseg
].reloc
;
885 switch (hr
->r
.length
) {
888 if (offset
< -127 || offset
> 128)
890 "warning: relocation out of range "
891 "at %s(%02x:%08"PRIx32
")\n", cur
->name
,
892 (int)hr
->r
.segment
, hr
->r
.offset
);
893 *data
= (char)offset
;
896 offset
+= *(int16_t *)data
;
897 if (offset
< -32767 || offset
> 32768)
899 "warning: relocation out of range "
900 "at %s(%02x:%08"PRIx32
")\n", cur
->name
,
901 (int)hr
->r
.segment
, hr
->r
.offset
);
902 *(int16_t *)data
= (int16_t)offset
;
905 *(int32_t *)data
+= offset
;
906 /* we can't easily detect overflow on this one */
911 * If the relocation was relative between two symbols in
912 * the same segment, then we're done.
914 * Otherwise, we need to output a new relocation record
915 * with the references updated segment and offset...
917 if (!isrelative
|| cur
->seginfo
[localseg
].dest_seg
!= seg
) {
918 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
919 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
922 hr
->r
.segment
+= RDOFF_RELATIVEMASK
;
923 rdfaddheader(rdfheader
, hr
);
927 case RDFREC_IMPORT
: /* import symbol */
928 case RDFREC_FARIMPORT
:
930 * scan the global symbol table for the symbol
931 * and associate its location with the segment number
934 se
= symtabFind(symtab
, hr
->i
.label
);
935 if (!se
|| se
->segment
== -1) {
936 if (!options
.dynalink
&& !(hr
->i
.flags
& SYM_IMPORT
)) {
938 "error: unresolved reference to `%s'"
939 " in module `%s'\n", hr
->i
.label
,
944 * we need to allocate a segment number for this
945 * symbol, and store it in the symbol table for
949 se
= malloc(sizeof(*se
));
951 fprintf(stderr
, "ldrdf: out of memory\n");
954 se
->name
= strdup(hr
->i
.label
);
956 se
->segment
= availableseg
++;
958 symtabInsert(symtab
, se
);
960 se
->segment
= availableseg
++;
964 * output a header record that imports it to the
965 * recently allocated segment number...
968 newrec
.i
.segment
= se
->segment
;
969 rdfaddheader(rdfheader
, &newrec
);
972 add_seglocation(&segs
, hr
->i
.segment
, se
->segment
,
976 case RDFREC_GLOBAL
: /* export symbol */
978 * need to insert an export for this symbol into the new
979 * header, unless we're stripping symbols. Even if we're
980 * stripping, put the symbol if it's marked as SYM_GLOBAL.
982 if (options
.strip
&& !(hr
->e
.flags
& SYM_GLOBAL
))
985 if (hr
->e
.segment
== 2) {
987 offset
= cur
->bss_reloc
;
989 localseg
= rdffindsegment(&cur
->f
, hr
->e
.segment
);
990 if (localseg
== -1) {
991 fprintf(stderr
, "%s: exported symbol `%s' from "
992 "unrecognised segment\n", cur
->name
,
997 offset
= cur
->seginfo
[localseg
].reloc
;
998 seg
= cur
->seginfo
[localseg
].dest_seg
;
1001 hr
->e
.segment
= seg
;
1002 hr
->e
.offset
+= offset
;
1003 rdfaddheader(rdfheader
, hr
);
1006 case RDFREC_MODNAME
: /* module name */
1008 * Insert module name record if export symbols
1010 * If module name begins with '$' - insert it anyway.
1012 if (options
.strip
&& hr
->m
.modname
[0] != '$')
1014 rdfaddheader(rdfheader
, hr
);
1017 case RDFREC_DLL
: /* DLL name */
1019 * Insert DLL name if it begins with '$'
1021 if (hr
->d
.libname
[0] != '$')
1023 rdfaddheader(rdfheader
, hr
);
1026 case RDFREC_SEGRELOC
: /* segment fixup */
1028 * modify the segment numbers if necessary, and
1029 * pass straight through to the output module header
1033 if (hr
->r
.segment
== 2) {
1034 fprintf(stderr
, "%s: segment fixup in BSS section\n",
1039 localseg
= rdffindsegment(&cur
->f
, hr
->r
.segment
);
1040 if (localseg
== -1) {
1041 fprintf(stderr
, "%s: segment fixup in unrecognised"
1042 " segment (%d)\n", cur
->name
, hr
->r
.segment
);
1046 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
1047 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
1049 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
1050 fprintf(stderr
, "%s: segment fixup to undefined "
1051 "segment %04x\n", cur
->name
,
1057 rdfaddheader(rdfheader
, hr
);
1060 case RDFREC_COMMON
: /* Common variable */
1061 /* Is this symbol already in the table? */
1062 se
= symtabFind(symtab
, hr
->c
.label
);
1064 printf("%s is not in symtab yet\n", hr
->c
.label
);
1067 /* Add segment location */
1068 add_seglocation(&segs
, hr
->c
.segment
, se
->segment
,
1075 done_seglocations(&segs
);
1080 * combined BSS reservation for the entire results
1082 newrec
.type
= RDFREC_BSS
;
1083 newrec
.b
.reclen
= 4;
1084 newrec
.b
.amount
= bss_length
;
1085 rdfaddheader(rdfheader
, &newrec
);
1090 for (i
= 0; i
< nsegs
; i
++) {
1093 rdfaddsegment(rdfheader
, outputseg
[i
].length
);
1096 rdfwriteheader(f
, rdfheader
);
1097 rdfdoneheader(rdfheader
);
1100 * Step through the segments, one at a time, writing out into
1103 for (i
= 0; i
< nsegs
; i
++) {
1110 s
= translateint16_t(outputseg
[i
].type
);
1111 fwrite(&s
, 2, 1, f
);
1112 s
= translateint16_t(outputseg
[i
].number
);
1113 fwrite(&s
, 2, 1, f
);
1114 s
= translateint16_t(outputseg
[i
].reserved
);
1115 fwrite(&s
, 2, 1, f
);
1116 l
= translateint32_t(outputseg
[i
].length
);
1117 fwrite(&l
, 4, 1, f
);
1119 fwrite(outputseg
[i
].data
, outputseg
[i
].length
, 1, f
);
1122 fwrite("\0\0\0\0\0\0\0\0\0\0", 10, 1, f
);
1125 /* =========================================================================
1132 " ldrdf [options] object modules ... [-llibrary ...]\n"
1135 " -v[=n] increase verbosity by 1, or set it to n\n"
1136 " -a nn set segment alignment value (default 16)\n"
1137 " -s strip public symbols\n"
1138 " -dy Unix-style dynamic linking\n"
1139 " -o name write output in file 'name'\n"
1140 " -j path specify objects search path\n"
1141 " -L path specify libraries search path\n"
1142 " -g file embed 'file' as a first header record with type 'generic'\n");
1146 int main(int argc
, char **argv
)
1148 char *outname
= "aout.rdf";
1149 int moduleloaded
= 0;
1150 char *respstrings
[128] = { 0, };
1152 options
.verbose
= 0;
1154 options
.dynalink
= 0;
1157 error_file
= stderr
;
1162 while (argc
&& *argv
&& **argv
== '-' && argv
[0][1] != 'l') {
1163 switch (argv
[0][1]) {
1165 printf("ldrdf (linker for RDF files) version " LDRDF_VERSION
1167 printf("RDOFF2 revision %s\n", RDOFF2_REVISION
);
1170 if (argv
[0][2] == '=') {
1171 options
.verbose
= argv
[0][3] - '0';
1172 if (options
.verbose
< 0 || options
.verbose
> 9) {
1174 "ldrdf: verbosity level must be a number"
1175 " between 0 and 9\n");
1182 options
.align
= atoi(argv
[1]);
1183 if (options
.align
<= 0) {
1185 "ldrdf: -a expects a positive number argument\n");
1194 if (argv
[0][2] == 'y')
1195 options
.dynalink
= 1;
1203 options
.objpath
= 1;
1209 "ldrdf: more than one objects search path specified\n");
1214 options
.libpath
= 1;
1220 "ldrdf: more than one libraries search path specified\n");
1228 options
.respfile
= 1;
1229 if (argv
[1] != NULL
)
1230 f
= fopen(argv
[1], "r");
1233 "ldrdf: no response file name specified\n");
1239 "ldrdf: unable to open response file\n");
1244 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1248 if ((p
= strchr(buf
, '\n')) != NULL
)
1251 fprintf(stderr
, "ldrdf: too many input files\n");
1254 *(respstrings
+ i
) = newstr(buf
);
1260 options
.stderr_redir
= 1;
1261 error_file
= stdout
;
1264 generic_rec_file
= argv
[1];
1273 if (options
.verbose
> 4) {
1274 printf("ldrdf invoked with options:\n");
1275 printf(" section alignment: %d bytes\n", options
.align
);
1276 printf(" output name: `%s'\n", outname
);
1278 printf(" strip symbols\n");
1279 if (options
.dynalink
)
1280 printf(" Unix-style dynamic linking\n");
1281 if (options
.objpath
)
1282 printf(" objects search path: %s\n", objpath
);
1283 if (options
.libpath
)
1284 printf(" libraries search path: %s\n", libpath
);
1288 symtab
= symtabNew();
1292 fprintf(stderr
, "ldrdf: out of memory\n");
1301 if (!strncmp(*argv
, "-l", 2)) {
1302 if (libpath
&& (argv
[0][2] != '/'))
1303 add_library(newstrcat(libpath
, *argv
+ 2));
1305 add_library(*argv
+ 2);
1307 if (objpath
&& (argv
[0][0] != '/'))
1308 loadmodule(newstrcat(objpath
, *argv
));
1316 if (!moduleloaded
) {
1317 printf("ldrdf: nothing to do. ldrdf -h for usage\n");
1323 if (options
.verbose
> 2) {
1324 printf("symbol table:\n");
1325 symtabDump(symtab
, stdout
);
1328 write_output(outname
);