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.
42 #define LDRDF_VERSION "1.07"
44 /* #define STINGY_MEMORY */
46 /* =======================================================================
47 * Types & macros that are private to this program
50 struct segment_infonode
{
51 int dest_seg
; /* output segment to be placed into, -1 to
52 skip linking this segment */
53 int32_t reloc
; /* segment's relocation factor */
57 rdffile f
; /* the RDOFF file structure */
58 struct segment_infonode seginfo
[RDF_MAXSEGS
]; /* what are we doing
62 struct modulenode
*next
;
68 #define newstr(str) strcpy(malloc(strlen(str) + 1),str)
69 #define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1)+strlen(s2)+1),s1),s2)
71 /* ==========================================================================
72 * Function prototypes of private utility functions
75 void processmodule(const char *filename
, struct modulenode
*mod
);
76 int allocnewseg(uint16_t type
, uint16_t reserved
);
77 int findsegment(uint16_t type
, uint16_t reserved
);
78 void symtab_add(const char *symbol
, int segment
, int32_t offset
);
79 int symtab_get(const char *symbol
, int *segment
, int32_t *offset
);
81 /* =========================================================================
82 * Global data structures.
85 /* a linked list of modules that will be included in the output */
86 struct modulenode
*modules
= NULL
;
87 struct modulenode
*lastmodule
= NULL
;
89 /* a linked list of libraries to be searched for unresolved imported symbols */
90 struct librarynode
*libraries
= NULL
;
91 struct librarynode
*lastlib
= NULL
;
93 /* the symbol table */
96 /* objects search path */
99 /* libraries search path */
100 char *libpath
= NULL
;
102 /* file to embed as a generic record */
103 char *generic_rec_file
= NULL
;
106 static FILE *error_file
;
108 /* the header of the output file, built up stage by stage */
109 rdf_headerbuf
*newheader
= NULL
;
111 /* The current state of segment allocation, including information about
112 * which output segment numbers have been allocated, and their types and
113 * amount of data which has already been allocated inside them.
115 struct SegmentHeaderRec outputseg
[RDF_MAXSEGS
];
119 /* global options which affect how the program behaves */
120 struct ldrdfoptions
{
131 int errorcount
= 0; /* determines main program exit status */
133 /* =========================================================================
140 * sets up segments 0, 1, and 2, the initial code data and bss segments
145 outputseg
[0].type
= 1;
146 outputseg
[0].number
= 0;
147 outputseg
[0].reserved
= 0;
148 outputseg
[0].length
= 0;
149 outputseg
[1].type
= 2;
150 outputseg
[1].number
= 1;
151 outputseg
[1].reserved
= 0;
152 outputseg
[1].length
= 0;
153 outputseg
[2].type
= 0xFFFF; /* reserved segment type */
154 outputseg
[2].number
= 2;
155 outputseg
[2].reserved
= 0;
156 outputseg
[2].length
= 0;
163 * Determine the characteristics of a module, and decide what to do with
164 * each segment it contains (including determining destination segments and
165 * relocation factors for segments that are kept).
167 void loadmodule(const char *filename
)
170 printf("loading `%s'\n", filename
);
172 /* allocate a new module entry on the end of the modules list */
174 modules
= malloc(sizeof(*modules
));
175 lastmodule
= modules
;
177 lastmodule
->next
= malloc(sizeof(*modules
));
178 lastmodule
= lastmodule
->next
;
182 fprintf(stderr
, "ldrdf: out of memory\n");
186 /* open the file using 'rdfopen', which returns nonzero on error */
187 if (rdfopen(&lastmodule
->f
, filename
) != 0) {
188 rdfperror("ldrdf", filename
);
193 * store information about the module, and determine what segments
194 * it contains, and what we should do with them (determine relocation
195 * factor if we decide to keep them)
197 lastmodule
->header
= NULL
;
198 lastmodule
->name
= strdup(filename
);
199 lastmodule
->next
= NULL
;
201 processmodule(filename
, lastmodule
);
207 * step through each segment, determine what exactly we're doing with
208 * it, and if we intend to keep it, determine (a) which segment to
209 * put it in and (b) whereabouts in that segment it will end up.
210 * (b) is fairly easy, because we're now keeping track of how big each
211 * segment in our output file is...
213 void processmodule(const char *filename
, struct modulenode
*mod
)
215 struct segconfig sconf
;
219 int32_t bssamount
= 0;
220 int bss_was_referenced
= 0;
222 memset(&sconf
, 0, sizeof sconf
);
224 for (seg
= 0; seg
< mod
->f
.nsegs
; seg
++) {
226 * get the segment configuration for this type from the segment
227 * table. getsegconfig() is a macro, defined in ldsegs.h.
229 getsegconfig(sconf
, mod
->f
.seg
[seg
].type
);
231 if (options
.verbose
> 1) {
232 printf("%s %04x [%04x:%10s] ", filename
,
233 mod
->f
.seg
[seg
].number
, mod
->f
.seg
[seg
].type
,
237 * sconf->dowhat tells us what to do with a segment of this type.
239 switch (sconf
.dowhat
) {
242 * Set destination segment to -1, to indicate that this segment
243 * should be ignored for the purpose of output, ie it is left
244 * out of the linked executable.
246 mod
->seginfo
[seg
].dest_seg
= -1;
247 if (options
.verbose
> 1)
253 * The configuration tells us to create a new segment for
254 * each occurrence of this segment type.
256 outseg
= allocnewseg(sconf
.mergetype
,
257 mod
->f
.seg
[seg
].reserved
);
258 mod
->seginfo
[seg
].dest_seg
= outseg
;
259 mod
->seginfo
[seg
].reloc
= 0;
260 outputseg
[outseg
].length
= mod
->f
.seg
[seg
].length
;
261 if (options
.verbose
> 1)
262 printf("=> %04x:%08"PRIx32
" (+%04"PRIx32
")\n", outseg
,
263 mod
->seginfo
[seg
].reloc
, mod
->f
.seg
[seg
].length
);
268 * The configuration tells us to merge the segment with
269 * a previously existing segment of type 'sconf.mergetype',
270 * if one exists. Otherwise a new segment is created.
271 * This is handled transparently by 'findsegment()'.
273 outseg
= findsegment(sconf
.mergetype
,
274 mod
->f
.seg
[seg
].reserved
);
275 mod
->seginfo
[seg
].dest_seg
= outseg
;
278 * We need to add alignment to these segments.
280 if (outputseg
[outseg
].length
% options
.align
!= 0)
281 outputseg
[outseg
].length
+=
283 (outputseg
[outseg
].length
% options
.align
);
285 mod
->seginfo
[seg
].reloc
= outputseg
[outseg
].length
;
286 outputseg
[outseg
].length
+= mod
->f
.seg
[seg
].length
;
288 if (options
.verbose
> 1)
289 printf("=> %04x:%08"PRIx32
" (+%04"PRIx32
")\n", outseg
,
290 mod
->seginfo
[seg
].reloc
, mod
->f
.seg
[seg
].length
);
296 * extract symbols from the header, and dump them into the
299 header
= malloc(mod
->f
.header_len
);
301 fprintf(stderr
, "ldrdf: not enough memory\n");
304 if (rdfloadseg(&mod
->f
, RDOFF_HEADER
, header
)) {
305 rdfperror("ldrdf", filename
);
309 while ((hr
= rdfgetheaderrec(&mod
->f
))) {
311 case RDFREC_IMPORT
: /* imported symbol */
312 case RDFREC_FARIMPORT
:
313 /* Define with seg = -1 */
314 symtab_add(hr
->i
.label
, -1, 0);
317 case RDFREC_GLOBAL
:{ /* exported symbol */
321 if (hr
->e
.segment
== 2) {
322 bss_was_referenced
= 1;
323 destreloc
= bss_length
;
324 if (destreloc
% options
.align
!= 0)
326 options
.align
- (destreloc
% options
.align
);
330 mod
->seginfo
[(int)hr
->e
.segment
].dest_seg
) == -1)
332 destreloc
= mod
->seginfo
[(int)hr
->e
.segment
].reloc
;
334 symtab_add(hr
->e
.label
, destseg
, destreloc
+ hr
->e
.offset
);
338 case RDFREC_BSS
: /* BSS reservation */
340 * first, amalgamate all BSS reservations in this module
341 * into one, because we allow this in the output format.
343 bssamount
+= hr
->b
.amount
;
346 case RDFREC_COMMON
:{ /* Common variable */
347 symtabEnt
*ste
= symtabFind(symtab
, hr
->c
.label
);
349 /* Is the symbol already in the table? */
353 /* Align the variable */
354 if (bss_length
% hr
->c
.align
!= 0)
355 bss_length
+= hr
->c
.align
- (bss_length
% hr
->c
.align
);
356 if (options
.verbose
> 1) {
357 printf("%s %04x common '%s' => 0002:%08"PRIx32
" (+%04"PRIx32
")\n",
358 filename
, hr
->c
.segment
, hr
->c
.label
,
359 bss_length
, hr
->c
.size
);
362 symtab_add(hr
->c
.label
, 2, bss_length
);
363 mod
->bss_reloc
= bss_length
;
364 bss_length
+= hr
->c
.size
;
370 if (bssamount
!= 0 || bss_was_referenced
) {
372 * handle the BSS segment - first pad the existing bss length
373 * to the correct alignment, then store the length in bss_reloc
374 * for this module. Then add this module's BSS length onto
377 if (bss_length
% options
.align
!= 0)
378 bss_length
+= options
.align
- (bss_length
% options
.align
);
380 mod
->bss_reloc
= bss_length
;
381 if (options
.verbose
> 1) {
382 printf("%s 0002 [ BSS] => 0002:%08"PRIx32
" (+%04"PRIx32
")\n",
383 filename
, bss_length
, bssamount
);
385 bss_length
+= bssamount
;
389 * we free the header buffer here, to save memory later.
390 * this isn't efficient, but probably halves the memory usage
393 mod
->f
.header_loc
= NULL
;
401 * Return 1 if a given module is in the list, 0 otherwise.
403 int lookformodule(const char *name
)
405 struct modulenode
*curr
= modules
;
408 if (!strcmp(name
, curr
->name
))
419 * These functions manipulate the array of output segments, and are used
420 * by processmodule(). allocnewseg() allocates a segment in the array,
421 * initialising it to be empty. findsegment() first scans the array for
422 * a segment of the type requested, and if one isn't found allocates a
425 int allocnewseg(uint16_t type
, uint16_t reserved
)
427 outputseg
[nsegs
].type
= type
;
428 outputseg
[nsegs
].number
= nsegs
;
429 outputseg
[nsegs
].reserved
= reserved
;
430 outputseg
[nsegs
].length
= 0;
431 outputseg
[nsegs
].offset
= 0;
432 outputseg
[nsegs
].data
= NULL
;
437 int findsegment(uint16_t type
, uint16_t reserved
)
441 for (i
= 0; i
< nsegs
; i
++)
442 if (outputseg
[i
].type
== type
)
445 return allocnewseg(type
, reserved
);
451 * inserts a symbol into the global symbol table, which associates symbol
452 * names either with addresses, or a marker that the symbol hasn't been
453 * resolved yet, or possibly that the symbol has been defined as
454 * contained in a dynamic [load time/run time] linked library.
456 * segment = -1 => not yet defined
457 * segment = -2 => defined as dll symbol
459 * If the symbol is already defined, and the new segment >= 0, then
460 * if the original segment was < 0 the symbol is redefined, otherwise
461 * a duplicate symbol warning is issued. If new segment == -1, this
462 * routine won't change a previously existing symbol. It will change
463 * to segment = -2 only if the segment was previously < 0.
465 void symtab_add(const char *symbol
, int segment
, int32_t offset
)
469 ste
= symtabFind(symtab
, symbol
);
471 if (ste
->segment
>= 0) {
473 * symbol previously defined
477 fprintf(error_file
, "warning: `%s' redefined\n", symbol
);
482 * somebody wanted the symbol, and put an undefined symbol
483 * marker into the table
488 * we have more information now - update the symbol's entry
490 ste
->segment
= segment
;
491 ste
->offset
= offset
;
496 * this is the first declaration of this symbol
498 ste
= malloc(sizeof(symtabEnt
));
500 fprintf(stderr
, "ldrdf: out of memory\n");
503 ste
->name
= strdup(symbol
);
504 ste
->segment
= segment
;
505 ste
->offset
= offset
;
507 symtabInsert(symtab
, ste
);
513 * Retrieves the values associated with a symbol. Undefined symbols
514 * are assumed to have -1:0 associated. Returns 1 if the symbol was
515 * successfully located.
517 int symtab_get(const char *symbol
, int *segment
, int32_t *offset
)
519 symtabEnt
*ste
= symtabFind(symtab
, symbol
);
525 *segment
= ste
->segment
;
526 *offset
= ste
->offset
;
534 * checks that a library can be opened and is in the correct format,
535 * then adds it to the linked list of libraries.
537 void add_library(const char *name
)
539 if (rdl_verify(name
)) {
540 rdl_perror("ldrdf", name
);
545 lastlib
= libraries
= malloc(sizeof(*libraries
));
547 fprintf(stderr
, "ldrdf: out of memory\n");
551 lastlib
->next
= malloc(sizeof(*libraries
));
552 if (!lastlib
->next
) {
553 fprintf(stderr
, "ldrdf: out of memory\n");
556 lastlib
= lastlib
->next
;
558 lastlib
->next
= NULL
;
559 if (rdl_open(lastlib
, name
)) {
560 rdl_perror("ldrdf", name
);
569 * scans through the list of libraries, attempting to match symbols
570 * defined in library modules against symbols that are referenced but
571 * not defined (segment = -1 in the symbol table)
573 * returns 1 if any extra library modules are included, indicating that
574 * another pass through the library list should be made (possibly).
576 int search_libraries()
578 struct librarynode
*cur
;
584 int doneanything
= 0, pass
= 1, keepfile
;
590 if (options
.verbose
> 2)
591 printf("scanning library `%s', pass %d...\n", cur
->name
, pass
);
593 for (i
= 0; rdl_openmodule(cur
, i
, &f
) == 0; i
++) {
594 if (pass
== 2 && lookformodule(f
.name
))
597 if (options
.verbose
> 3)
598 printf(" looking in module `%s'\n", f
.name
);
600 header
= malloc(f
.header_len
);
602 fprintf(stderr
, "ldrdf: not enough memory\n");
605 if (rdfloadseg(&f
, RDOFF_HEADER
, header
)) {
606 rdfperror("ldrdf", f
.name
);
613 while ((hr
= rdfgetheaderrec(&f
))) {
614 /* We're only interested in exports, so skip others */
615 if (hr
->type
!= RDFREC_GLOBAL
)
619 * If the symbol is marked as SYM_GLOBAL, somebody will be
620 * definitely interested in it..
622 if ((hr
->e
.flags
& SYM_GLOBAL
) == 0) {
624 * otherwise the symbol is just public. Find it in
625 * the symbol table. If the symbol isn't defined, we
626 * aren't interested, so go on to the next.
627 * If it is defined as anything but -1, we're also not
628 * interested. But if it is defined as -1, insert this
629 * module into the list of modules to use, and go
630 * immediately on to the next module...
632 if (!symtab_get(hr
->e
.label
, &segment
, &offset
)
641 * as there are undefined symbols, we can assume that
642 * there are modules on the module list by the time
645 lastmodule
->next
= malloc(sizeof(*lastmodule
->next
));
646 if (!lastmodule
->next
) {
647 fprintf(stderr
, "ldrdf: not enough memory\n");
650 lastmodule
= lastmodule
->next
;
651 memcpy(&lastmodule
->f
, &f
, sizeof(f
));
652 lastmodule
->name
= strdup(f
.name
);
653 lastmodule
->next
= NULL
;
654 processmodule(f
.name
, lastmodule
);
663 if (rdl_error
!= 0 && rdl_error
!= RDL_ENOTFOUND
)
664 rdl_perror("ldrdf", cur
->name
);
667 if (cur
== NULL
&& pass
== 1) {
679 * this takes the linked list of modules, and walks through it, merging
680 * all the modules into a single output module, and then writes this to a
683 void write_output(const char *filename
)
686 rdf_headerbuf
*rdfheader
;
687 struct modulenode
*cur
;
688 int i
, availableseg
, seg
, localseg
, isrelative
;
690 rdfheaderrec
*hr
, newrec
;
696 if ((f
= fopen(filename
, "wb")) == NULL
) {
697 fprintf(stderr
, "ldrdf: couldn't open %s for output\n", filename
);
700 if ((rdfheader
= rdfnewheader()) == NULL
) {
701 fprintf(stderr
, "ldrdf: out of memory\n");
706 * If '-g' option was given, first record in output file will be a
707 * `generic' record, filled with a given file content.
708 * This can be useful, for example, when constructing multiboot
711 if (generic_rec_file
) {
715 printf("\nadding generic record from binary file %s\n",
718 hr
= (rdfheaderrec
*) malloc(sizeof(struct GenericRec
));
719 if ((ff
= fopen(generic_rec_file
, "r")) == NULL
) {
720 fprintf(stderr
, "ldrdf: couldn't open %s for input\n",
724 i
= fread(hr
->g
.data
, 1, sizeof(hr
->g
.data
), ff
);
725 fseek(ff
, 0, SEEK_END
);
726 if (ftell(ff
) > (long)sizeof(hr
->g
.data
)) {
728 "warning: maximum generic record size is %u, "
729 "rest of file ignored\n",
730 (unsigned int)sizeof(hr
->g
.data
));
736 rdfaddheader(rdfheader
, hr
);
741 printf("\nbuilding output module (%d segments)\n", nsegs
);
744 * Allocate the memory for the segments. We may be better off
745 * building the output module one segment at a time when running
746 * under 16 bit DOS, but that would be a slower way of doing this.
747 * And you could always use DJGPP...
749 for (i
= 0; i
< nsegs
; i
++) {
750 outputseg
[i
].data
= NULL
;
751 if (!outputseg
[i
].length
)
753 outputseg
[i
].data
= malloc(outputseg
[i
].length
);
754 if (!outputseg
[i
].data
) {
755 fprintf(stderr
, "ldrdf: out of memory\n");
761 * initialise availableseg, used to allocate segment numbers for
762 * imported and exported labels...
764 availableseg
= nsegs
;
767 * Step through the modules, performing required actions on each one
769 for (cur
= modules
; cur
; cur
= cur
->next
) {
771 * Read the actual segment contents into the correct places in
772 * the newly allocated segments
775 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
776 int dest
= cur
->seginfo
[i
].dest_seg
;
780 if (rdfloadseg(&cur
->f
, i
,
781 outputseg
[dest
].data
+ cur
->seginfo
[i
].reloc
)) {
782 rdfperror("ldrdf", cur
->name
);
788 * Perform fixups, and add new header records where required
791 header
= malloc(cur
->f
.header_len
);
793 fprintf(stderr
, "ldrdf: out of memory\n");
797 if (cur
->f
.header_loc
)
798 rdfheaderrewind(&cur
->f
);
799 else if (rdfloadseg(&cur
->f
, RDOFF_HEADER
, header
)) {
800 rdfperror("ldrdf", cur
->name
);
805 * we need to create a local segment number -> location
806 * table for the segments in this module.
808 init_seglocations(&segs
);
809 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
810 add_seglocation(&segs
, cur
->f
.seg
[i
].number
,
811 cur
->seginfo
[i
].dest_seg
,
812 cur
->seginfo
[i
].reloc
);
815 * and the BSS segment (doh!)
817 add_seglocation(&segs
, 2, 2, cur
->bss_reloc
);
819 while ((hr
= rdfgetheaderrec(&cur
->f
))) {
821 case RDFREC_RELOC
: /* relocation record - need to do a fixup */
823 * First correct the offset stored in the segment from
824 * the start of the segment (which may well have changed).
826 * To do this we add to the number stored the relocation
827 * factor associated with the segment that contains the
830 * The relocation could be a relative relocation, in which
831 * case we have to first subtract the amount we've relocated
832 * the containing segment by.
834 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
836 "%s: reloc to undefined segment %04x\n",
837 cur
->name
, (int)hr
->r
.refseg
);
843 (hr
->r
.segment
& RDOFF_RELATIVEMASK
) ==
845 hr
->r
.segment
&= (RDOFF_RELATIVEMASK
- 1);
847 if (hr
->r
.segment
== 2 ||
849 rdffindsegment(&cur
->f
, hr
->r
.segment
)) == -1) {
850 fprintf(stderr
, "%s: reloc from %s segment (%d)\n",
852 hr
->r
.segment
== 2 ? "BSS" : "unknown",
858 if (hr
->r
.length
!= 1 && hr
->r
.length
!= 2 &&
860 fprintf(stderr
, "%s: nonstandard length reloc "
861 "(%d bytes)\n", cur
->name
, hr
->r
.length
);
867 * okay, now the relocation is in the segment pointed to by
868 * cur->seginfo[localseg], and we know everything else is
869 * okay to go ahead and do the relocation
871 data
= outputseg
[cur
->seginfo
[localseg
].dest_seg
].data
;
872 data
+= cur
->seginfo
[localseg
].reloc
+ hr
->r
.offset
;
875 * data now points to the reference that needs
876 * relocation. Calculate the relocation factor.
878 * offset of referred object in segment [in offset]
879 * (- relocation of localseg, if ref is relative)
880 * For simplicity, the result is stored in 'offset'.
881 * Then add 'offset' onto the value at data.
885 offset
-= cur
->seginfo
[localseg
].reloc
;
886 switch (hr
->r
.length
) {
889 if (offset
< -127 || offset
> 128)
891 "warning: relocation out of range "
892 "at %s(%02x:%08"PRIx32
")\n", cur
->name
,
893 (int)hr
->r
.segment
, hr
->r
.offset
);
894 *data
= (char)offset
;
897 offset
+= *(int16_t *)data
;
898 if (offset
< -32767 || offset
> 32768)
900 "warning: relocation out of range "
901 "at %s(%02x:%08"PRIx32
")\n", cur
->name
,
902 (int)hr
->r
.segment
, hr
->r
.offset
);
903 *(int16_t *)data
= (int16_t)offset
;
906 *(int32_t *)data
+= offset
;
907 /* we can't easily detect overflow on this one */
912 * If the relocation was relative between two symbols in
913 * the same segment, then we're done.
915 * Otherwise, we need to output a new relocation record
916 * with the references updated segment and offset...
918 if (!isrelative
|| cur
->seginfo
[localseg
].dest_seg
!= seg
) {
919 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
920 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
923 hr
->r
.segment
+= RDOFF_RELATIVEMASK
;
924 rdfaddheader(rdfheader
, hr
);
928 case RDFREC_IMPORT
: /* import symbol */
929 case RDFREC_FARIMPORT
:
931 * scan the global symbol table for the symbol
932 * and associate its location with the segment number
935 se
= symtabFind(symtab
, hr
->i
.label
);
936 if (!se
|| se
->segment
== -1) {
937 if (!options
.dynalink
&& !(hr
->i
.flags
& SYM_IMPORT
)) {
939 "error: unresolved reference to `%s'"
940 " in module `%s'\n", hr
->i
.label
,
945 * we need to allocate a segment number for this
946 * symbol, and store it in the symbol table for
950 se
= malloc(sizeof(*se
));
952 fprintf(stderr
, "ldrdf: out of memory\n");
955 se
->name
= strdup(hr
->i
.label
);
957 se
->segment
= availableseg
++;
959 symtabInsert(symtab
, se
);
961 se
->segment
= availableseg
++;
965 * output a header record that imports it to the
966 * recently allocated segment number...
969 newrec
.i
.segment
= se
->segment
;
970 rdfaddheader(rdfheader
, &newrec
);
973 add_seglocation(&segs
, hr
->i
.segment
, se
->segment
,
977 case RDFREC_GLOBAL
: /* export symbol */
979 * need to insert an export for this symbol into the new
980 * header, unless we're stripping symbols. Even if we're
981 * stripping, put the symbol if it's marked as SYM_GLOBAL.
983 if (options
.strip
&& !(hr
->e
.flags
& SYM_GLOBAL
))
986 if (hr
->e
.segment
== 2) {
988 offset
= cur
->bss_reloc
;
990 localseg
= rdffindsegment(&cur
->f
, hr
->e
.segment
);
991 if (localseg
== -1) {
992 fprintf(stderr
, "%s: exported symbol `%s' from "
993 "unrecognised segment\n", cur
->name
,
998 offset
= cur
->seginfo
[localseg
].reloc
;
999 seg
= cur
->seginfo
[localseg
].dest_seg
;
1002 hr
->e
.segment
= seg
;
1003 hr
->e
.offset
+= offset
;
1004 rdfaddheader(rdfheader
, hr
);
1007 case RDFREC_MODNAME
: /* module name */
1009 * Insert module name record if export symbols
1011 * If module name begins with '$' - insert it anyway.
1013 if (options
.strip
&& hr
->m
.modname
[0] != '$')
1015 rdfaddheader(rdfheader
, hr
);
1018 case RDFREC_DLL
: /* DLL name */
1020 * Insert DLL name if it begins with '$'
1022 if (hr
->d
.libname
[0] != '$')
1024 rdfaddheader(rdfheader
, hr
);
1027 case RDFREC_SEGRELOC
: /* segment fixup */
1029 * modify the segment numbers if necessary, and
1030 * pass straight through to the output module header
1034 if (hr
->r
.segment
== 2) {
1035 fprintf(stderr
, "%s: segment fixup in BSS section\n",
1040 localseg
= rdffindsegment(&cur
->f
, hr
->r
.segment
);
1041 if (localseg
== -1) {
1042 fprintf(stderr
, "%s: segment fixup in unrecognised"
1043 " segment (%d)\n", cur
->name
, hr
->r
.segment
);
1047 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
1048 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
1050 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
1051 fprintf(stderr
, "%s: segment fixup to undefined "
1052 "segment %04x\n", cur
->name
,
1058 rdfaddheader(rdfheader
, hr
);
1061 case RDFREC_COMMON
: /* Common variable */
1062 /* Is this symbol already in the table? */
1063 se
= symtabFind(symtab
, hr
->c
.label
);
1065 printf("%s is not in symtab yet\n", hr
->c
.label
);
1068 /* Add segment location */
1069 add_seglocation(&segs
, hr
->c
.segment
, se
->segment
,
1076 done_seglocations(&segs
);
1081 * combined BSS reservation for the entire results
1083 newrec
.type
= RDFREC_BSS
;
1084 newrec
.b
.reclen
= 4;
1085 newrec
.b
.amount
= bss_length
;
1086 rdfaddheader(rdfheader
, &newrec
);
1091 for (i
= 0; i
< nsegs
; i
++) {
1094 rdfaddsegment(rdfheader
, outputseg
[i
].length
);
1097 rdfwriteheader(f
, rdfheader
);
1098 rdfdoneheader(rdfheader
);
1101 * Step through the segments, one at a time, writing out into
1104 for (i
= 0; i
< nsegs
; i
++) {
1111 s
= translateint16_t(outputseg
[i
].type
);
1112 fwrite(&s
, 2, 1, f
);
1113 s
= translateint16_t(outputseg
[i
].number
);
1114 fwrite(&s
, 2, 1, f
);
1115 s
= translateint16_t(outputseg
[i
].reserved
);
1116 fwrite(&s
, 2, 1, f
);
1117 l
= translateint32_t(outputseg
[i
].length
);
1118 fwrite(&l
, 4, 1, f
);
1120 fwrite(outputseg
[i
].data
, outputseg
[i
].length
, 1, f
);
1126 /* =========================================================================
1133 " ldrdf [options] object modules ... [-llibrary ...]\n"
1136 " -v[=n] increase verbosity by 1, or set it to n\n"
1137 " -a nn set segment alignment value (default 16)\n"
1138 " -s strip public symbols\n"
1139 " -dy Unix-style dynamic linking\n"
1140 " -o name write output in file 'name'\n"
1141 " -j path specify objects search path\n"
1142 " -L path specify libraries search path\n"
1143 " -g file embed 'file' as a first header record with type 'generic'\n");
1147 int main(int argc
, char **argv
)
1149 char *outname
= "aout.rdf";
1150 int moduleloaded
= 0;
1151 char *respstrings
[128] = { 0, };
1153 options
.verbose
= 0;
1155 options
.dynalink
= 0;
1158 error_file
= stderr
;
1163 while (argc
&& *argv
&& **argv
== '-' && argv
[0][1] != 'l') {
1164 switch (argv
[0][1]) {
1166 printf("ldrdf (linker for RDF files) version " LDRDF_VERSION
1168 printf("RDOFF2 revision %s\n", RDOFF2_REVISION
);
1171 if (argv
[0][2] == '=') {
1172 options
.verbose
= argv
[0][3] - '0';
1173 if (options
.verbose
< 0 || options
.verbose
> 9) {
1175 "ldrdf: verbosity level must be a number"
1176 " between 0 and 9\n");
1183 options
.align
= atoi(argv
[1]);
1184 if (options
.align
<= 0) {
1186 "ldrdf: -a expects a positive number argument\n");
1195 if (argv
[0][2] == 'y')
1196 options
.dynalink
= 1;
1204 options
.objpath
= 1;
1210 "ldrdf: more than one objects search path specified\n");
1215 options
.libpath
= 1;
1221 "ldrdf: more than one libraries search path specified\n");
1229 options
.respfile
= 1;
1230 if (argv
[1] != NULL
)
1231 f
= fopen(argv
[1], "r");
1234 "ldrdf: no response file name specified\n");
1240 "ldrdf: unable to open response file\n");
1245 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1249 if ((p
= strchr(buf
, '\n')) != NULL
)
1252 fprintf(stderr
, "ldrdf: too many input files\n");
1255 *(respstrings
+ i
) = newstr(buf
);
1261 options
.stderr_redir
= 1;
1262 error_file
= stdout
;
1265 generic_rec_file
= argv
[1];
1274 if (options
.verbose
> 4) {
1275 printf("ldrdf invoked with options:\n");
1276 printf(" section alignment: %d bytes\n", options
.align
);
1277 printf(" output name: `%s'\n", outname
);
1279 printf(" strip symbols\n");
1280 if (options
.dynalink
)
1281 printf(" Unix-style dynamic linking\n");
1282 if (options
.objpath
)
1283 printf(" objects search path: %s\n", objpath
);
1284 if (options
.libpath
)
1285 printf(" libraries search path: %s\n", libpath
);
1289 symtab
= symtabNew();
1293 fprintf(stderr
, "ldrdf: out of memory\n");
1302 if (!strncmp(*argv
, "-l", 2)) {
1303 if (libpath
&& (argv
[0][2] != '/'))
1304 add_library(newstrcat(libpath
, *argv
+ 2));
1306 add_library(*argv
+ 2);
1308 if (objpath
&& (argv
[0][0] != '/'))
1309 loadmodule(newstrcat(objpath
, *argv
));
1317 if (!moduleloaded
) {
1318 printf("ldrdf: nothing to do. ldrdf -h for usage\n");
1324 if (options
.verbose
> 2) {
1325 printf("symbol table:\n");
1326 symtabDump(symtab
, stdout
);
1329 write_output(outname
);