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.
39 #define LDRDF_VERSION "1.07"
41 /* #define STINGY_MEMORY */
43 /* =======================================================================
44 * Types & macros that are private to this program
47 struct segment_infonode
{
48 int dest_seg
; /* output segment to be placed into, -1 to
49 skip linking this segment */
50 long reloc
; /* segment's relocation factor */
54 rdffile f
; /* the RDOFF file structure */
55 struct segment_infonode seginfo
[RDF_MAXSEGS
]; /* what are we doing
59 struct modulenode
*next
;
65 #define newstr(str) strcpy(malloc(strlen(str) + 1),str)
66 #define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1)+strlen(s2)+1),s1),s2)
68 /* ==========================================================================
69 * Function prototypes of private utility functions
72 void processmodule(const char *filename
, struct modulenode
*mod
);
73 int allocnewseg(uint16 type
, uint16 reserved
);
74 int findsegment(uint16 type
, uint16 reserved
);
75 void symtab_add(const char *symbol
, int segment
, long offset
);
76 int symtab_get(const char *symbol
, int *segment
, long *offset
);
78 /* =========================================================================
79 * Global data structures.
82 /* a linked list of modules that will be included in the output */
83 struct modulenode
*modules
= NULL
;
84 struct modulenode
*lastmodule
= NULL
;
86 /* a linked list of libraries to be searched for unresolved imported symbols */
87 struct librarynode
*libraries
= NULL
;
88 struct librarynode
*lastlib
= NULL
;
90 /* the symbol table */
93 /* objects search path */
96 /* libraries search path */
99 /* file to embed as a generic record */
100 char *generic_rec_file
= NULL
;
103 static FILE *error_file
;
105 /* the header of the output file, built up stage by stage */
106 rdf_headerbuf
*newheader
= NULL
;
108 /* The current state of segment allocation, including information about
109 * which output segment numbers have been allocated, and their types and
110 * amount of data which has already been allocated inside them.
112 struct SegmentHeaderRec outputseg
[RDF_MAXSEGS
];
116 /* global options which affect how the program behaves */
117 struct ldrdfoptions
{
128 int errorcount
= 0; /* determines main program exit status */
130 /* =========================================================================
137 * sets up segments 0, 1, and 2, the initial code data and bss segments
142 outputseg
[0].type
= 1;
143 outputseg
[0].number
= 0;
144 outputseg
[0].reserved
= 0;
145 outputseg
[0].length
= 0;
146 outputseg
[1].type
= 2;
147 outputseg
[1].number
= 1;
148 outputseg
[1].reserved
= 0;
149 outputseg
[1].length
= 0;
150 outputseg
[2].type
= 0xFFFF; /* reserved segment type */
151 outputseg
[2].number
= 2;
152 outputseg
[2].reserved
= 0;
153 outputseg
[2].length
= 0;
160 * Determine the characteristics of a module, and decide what to do with
161 * each segment it contains (including determining destination segments and
162 * relocation factors for segments that are kept).
164 void loadmodule(const char *filename
)
167 printf("loading `%s'\n", filename
);
169 /* allocate a new module entry on the end of the modules list */
171 modules
= malloc(sizeof(*modules
));
172 lastmodule
= modules
;
174 lastmodule
->next
= malloc(sizeof(*modules
));
175 lastmodule
= lastmodule
->next
;
179 fprintf(stderr
, "ldrdf: out of memory\n");
183 /* open the file using 'rdfopen', which returns nonzero on error */
184 if (rdfopen(&lastmodule
->f
, filename
) != 0) {
185 rdfperror("ldrdf", filename
);
190 * store information about the module, and determine what segments
191 * it contains, and what we should do with them (determine relocation
192 * factor if we decide to keep them)
194 lastmodule
->header
= NULL
;
195 lastmodule
->name
= strdup(filename
);
196 lastmodule
->next
= NULL
;
198 processmodule(filename
, lastmodule
);
204 * step through each segment, determine what exactly we're doing with
205 * it, and if we intend to keep it, determine (a) which segment to
206 * put it in and (b) whereabouts in that segment it will end up.
207 * (b) is fairly easy, because we're now keeping track of how big each
208 * segment in our output file is...
210 void processmodule(const char *filename
, struct modulenode
*mod
)
212 struct segconfig sconf
;
217 int bss_was_referenced
= 0;
219 for (seg
= 0; seg
< mod
->f
.nsegs
; seg
++) {
221 * get the segment configuration for this type from the segment
222 * table. getsegconfig() is a macro, defined in ldsegs.h.
224 getsegconfig(sconf
, mod
->f
.seg
[seg
].type
);
226 if (options
.verbose
> 1) {
227 printf("%s %04x [%04x:%10s] ", filename
,
228 mod
->f
.seg
[seg
].number
, mod
->f
.seg
[seg
].type
,
232 * sconf->dowhat tells us what to do with a segment of this type.
234 switch (sconf
.dowhat
) {
237 * Set destination segment to -1, to indicate that this segment
238 * should be ignored for the purpose of output, ie it is left
239 * out of the linked executable.
241 mod
->seginfo
[seg
].dest_seg
= -1;
242 if (options
.verbose
> 1)
248 * The configuration tells us to create a new segment for
249 * each occurrence of this segment type.
251 outseg
= allocnewseg(sconf
.mergetype
,
252 mod
->f
.seg
[seg
].reserved
);
253 mod
->seginfo
[seg
].dest_seg
= outseg
;
254 mod
->seginfo
[seg
].reloc
= 0;
255 outputseg
[outseg
].length
= mod
->f
.seg
[seg
].length
;
256 if (options
.verbose
> 1)
257 printf("=> %04x:%08lx (+%04lx)\n", outseg
,
258 mod
->seginfo
[seg
].reloc
, mod
->f
.seg
[seg
].length
);
263 * The configuration tells us to merge the segment with
264 * a previously existing segment of type 'sconf.mergetype',
265 * if one exists. Otherwise a new segment is created.
266 * This is handled transparently by 'findsegment()'.
268 outseg
= findsegment(sconf
.mergetype
,
269 mod
->f
.seg
[seg
].reserved
);
270 mod
->seginfo
[seg
].dest_seg
= outseg
;
273 * We need to add alignment to these segments.
275 if (outputseg
[outseg
].length
% options
.align
!= 0)
276 outputseg
[outseg
].length
+=
278 (outputseg
[outseg
].length
% options
.align
);
280 mod
->seginfo
[seg
].reloc
= outputseg
[outseg
].length
;
281 outputseg
[outseg
].length
+= mod
->f
.seg
[seg
].length
;
283 if (options
.verbose
> 1)
284 printf("=> %04x:%08lx (+%04lx)\n", outseg
,
285 mod
->seginfo
[seg
].reloc
, mod
->f
.seg
[seg
].length
);
291 * extract symbols from the header, and dump them into the
294 header
= malloc(mod
->f
.header_len
);
296 fprintf(stderr
, "ldrdf: not enough memory\n");
299 if (rdfloadseg(&mod
->f
, RDOFF_HEADER
, header
)) {
300 rdfperror("ldrdf", filename
);
304 while ((hr
= rdfgetheaderrec(&mod
->f
))) {
306 case RDFREC_IMPORT
: /* imported symbol */
307 case RDFREC_FARIMPORT
:
308 /* Define with seg = -1 */
309 symtab_add(hr
->i
.label
, -1, 0);
312 case RDFREC_GLOBAL
:{ /* exported symbol */
316 if (hr
->e
.segment
== 2) {
317 bss_was_referenced
= 1;
318 destreloc
= bss_length
;
319 if (destreloc
% options
.align
!= 0)
321 options
.align
- (destreloc
% options
.align
);
325 mod
->seginfo
[(int)hr
->e
.segment
].dest_seg
) == -1)
327 destreloc
= mod
->seginfo
[(int)hr
->e
.segment
].reloc
;
329 symtab_add(hr
->e
.label
, destseg
, destreloc
+ hr
->e
.offset
);
333 case RDFREC_BSS
: /* BSS reservation */
335 * first, amalgamate all BSS reservations in this module
336 * into one, because we allow this in the output format.
338 bssamount
+= hr
->b
.amount
;
341 case RDFREC_COMMON
:{ /* Common variable */
342 symtabEnt
*ste
= symtabFind(symtab
, hr
->c
.label
);
344 /* Is the symbol already in the table? */
348 /* Align the variable */
349 if (bss_length
% hr
->c
.align
!= 0)
350 bss_length
+= hr
->c
.align
- (bss_length
% hr
->c
.align
);
351 if (options
.verbose
> 1) {
352 printf("%s %04x common '%s' => 0002:%08lx (+%04lx)\n",
353 filename
, hr
->c
.segment
, hr
->c
.label
,
354 bss_length
, hr
->c
.size
);
357 symtab_add(hr
->c
.label
, 2, bss_length
);
358 mod
->bss_reloc
= bss_length
;
359 bss_length
+= hr
->c
.size
;
365 if (bssamount
!= 0 || bss_was_referenced
) {
367 * handle the BSS segment - first pad the existing bss length
368 * to the correct alignment, then store the length in bss_reloc
369 * for this module. Then add this module's BSS length onto
372 if (bss_length
% options
.align
!= 0)
373 bss_length
+= options
.align
- (bss_length
% options
.align
);
375 mod
->bss_reloc
= bss_length
;
376 if (options
.verbose
> 1) {
377 printf("%s 0002 [ BSS] => 0002:%08lx (+%04lx)\n",
378 filename
, bss_length
, bssamount
);
380 bss_length
+= bssamount
;
384 * we free the header buffer here, to save memory later.
385 * this isn't efficient, but probably halves the memory usage
388 mod
->f
.header_loc
= NULL
;
396 * Return 1 if a given module is in the list, 0 otherwise.
398 int lookformodule(const char *name
)
400 struct modulenode
*curr
= modules
;
403 if (!strcmp(name
, curr
->name
))
414 * These functions manipulate the array of output segments, and are used
415 * by processmodule(). allocnewseg() allocates a segment in the array,
416 * initialising it to be empty. findsegment() first scans the array for
417 * a segment of the type requested, and if one isn't found allocates a
420 int allocnewseg(uint16 type
, uint16 reserved
)
422 outputseg
[nsegs
].type
= type
;
423 outputseg
[nsegs
].number
= nsegs
;
424 outputseg
[nsegs
].reserved
= reserved
;
425 outputseg
[nsegs
].length
= 0;
426 outputseg
[nsegs
].offset
= 0;
427 outputseg
[nsegs
].data
= NULL
;
432 int findsegment(uint16 type
, uint16 reserved
)
436 for (i
= 0; i
< nsegs
; i
++)
437 if (outputseg
[i
].type
== type
)
440 return allocnewseg(type
, reserved
);
446 * inserts a symbol into the global symbol table, which associates symbol
447 * names either with addresses, or a marker that the symbol hasn't been
448 * resolved yet, or possibly that the symbol has been defined as
449 * contained in a dynamic [load time/run time] linked library.
451 * segment = -1 => not yet defined
452 * segment = -2 => defined as dll symbol
454 * If the symbol is already defined, and the new segment >= 0, then
455 * if the original segment was < 0 the symbol is redefined, otherwise
456 * a duplicate symbol warning is issued. If new segment == -1, this
457 * routine won't change a previously existing symbol. It will change
458 * to segment = -2 only if the segment was previously < 0.
460 void symtab_add(const char *symbol
, int segment
, long offset
)
464 ste
= symtabFind(symtab
, symbol
);
466 if (ste
->segment
>= 0) {
468 * symbol previously defined
472 fprintf(error_file
, "warning: `%s' redefined\n", symbol
);
477 * somebody wanted the symbol, and put an undefined symbol
478 * marker into the table
483 * we have more information now - update the symbol's entry
485 ste
->segment
= segment
;
486 ste
->offset
= offset
;
491 * this is the first declaration of this symbol
493 ste
= malloc(sizeof(symtabEnt
));
495 fprintf(stderr
, "ldrdf: out of memory\n");
498 ste
->name
= strdup(symbol
);
499 ste
->segment
= segment
;
500 ste
->offset
= offset
;
502 symtabInsert(symtab
, ste
);
508 * Retrieves the values associated with a symbol. Undefined symbols
509 * are assumed to have -1:0 associated. Returns 1 if the symbol was
510 * successfully located.
512 int symtab_get(const char *symbol
, int *segment
, long *offset
)
514 symtabEnt
*ste
= symtabFind(symtab
, symbol
);
520 *segment
= ste
->segment
;
521 *offset
= ste
->offset
;
529 * checks that a library can be opened and is in the correct format,
530 * then adds it to the linked list of libraries.
532 void add_library(const char *name
)
534 if (rdl_verify(name
)) {
535 rdl_perror("ldrdf", name
);
540 lastlib
= libraries
= malloc(sizeof(*libraries
));
542 fprintf(stderr
, "ldrdf: out of memory\n");
546 lastlib
->next
= malloc(sizeof(*libraries
));
547 if (!lastlib
->next
) {
548 fprintf(stderr
, "ldrdf: out of memory\n");
551 lastlib
= lastlib
->next
;
553 lastlib
->next
= NULL
;
554 if (rdl_open(lastlib
, name
)) {
555 rdl_perror("ldrdf", name
);
564 * scans through the list of libraries, attempting to match symbols
565 * defined in library modules against symbols that are referenced but
566 * not defined (segment = -1 in the symbol table)
568 * returns 1 if any extra library modules are included, indicating that
569 * another pass through the library list should be made (possibly).
571 int search_libraries()
573 struct librarynode
*cur
;
579 int doneanything
= 0, pass
= 1, keepfile
;
585 if (options
.verbose
> 2)
586 printf("scanning library `%s', pass %d...\n", cur
->name
, pass
);
588 for (i
= 0; rdl_openmodule(cur
, i
, &f
) == 0; i
++) {
589 if (pass
== 2 && lookformodule(f
.name
))
592 if (options
.verbose
> 3)
593 printf(" looking in module `%s'\n", f
.name
);
595 header
= malloc(f
.header_len
);
597 fprintf(stderr
, "ldrdf: not enough memory\n");
600 if (rdfloadseg(&f
, RDOFF_HEADER
, header
)) {
601 rdfperror("ldrdf", f
.name
);
608 while ((hr
= rdfgetheaderrec(&f
))) {
609 /* We're only interested in exports, so skip others */
610 if (hr
->type
!= RDFREC_GLOBAL
)
614 * If the symbol is marked as SYM_GLOBAL, somebody will be
615 * definitely interested in it..
617 if ((hr
->e
.flags
& SYM_GLOBAL
) == 0) {
619 * otherwise the symbol is just public. Find it in
620 * the symbol table. If the symbol isn't defined, we
621 * aren't interested, so go on to the next.
622 * If it is defined as anything but -1, we're also not
623 * interested. But if it is defined as -1, insert this
624 * module into the list of modules to use, and go
625 * immediately on to the next module...
627 if (!symtab_get(hr
->e
.label
, &segment
, &offset
)
636 * as there are undefined symbols, we can assume that
637 * there are modules on the module list by the time
640 lastmodule
->next
= malloc(sizeof(*lastmodule
->next
));
641 if (!lastmodule
->next
) {
642 fprintf(stderr
, "ldrdf: not enough memory\n");
645 lastmodule
= lastmodule
->next
;
646 memcpy(&lastmodule
->f
, &f
, sizeof(f
));
647 lastmodule
->name
= strdup(f
.name
);
648 lastmodule
->next
= NULL
;
649 processmodule(f
.name
, lastmodule
);
658 if (rdl_error
!= 0 && rdl_error
!= RDL_ENOTFOUND
)
659 rdl_perror("ldrdf", cur
->name
);
662 if (cur
== NULL
&& pass
== 1) {
674 * this takes the linked list of modules, and walks through it, merging
675 * all the modules into a single output module, and then writes this to a
678 void write_output(const char *filename
)
681 rdf_headerbuf
*rdfheader
;
682 struct modulenode
*cur
;
683 int i
, availableseg
, seg
, localseg
, isrelative
;
685 rdfheaderrec
*hr
, newrec
;
691 if ((f
= fopen(filename
, "wb")) == NULL
) {
692 fprintf(stderr
, "ldrdf: couldn't open %s for output\n", filename
);
695 if ((rdfheader
= rdfnewheader()) == NULL
) {
696 fprintf(stderr
, "ldrdf: out of memory\n");
701 * If '-g' option was given, first record in output file will be a
702 * `generic' record, filled with a given file content.
703 * This can be useful, for example, when constructing multiboot
706 if (generic_rec_file
) {
710 printf("\nadding generic record from binary file %s\n",
713 hr
= (rdfheaderrec
*) malloc(sizeof(struct GenericRec
));
714 if ((ff
= fopen(generic_rec_file
, "r")) == NULL
) {
715 fprintf(stderr
, "ldrdf: couldn't open %s for input\n",
719 i
= fread(hr
->g
.data
, 1, sizeof(hr
->g
.data
), ff
);
720 fseek(ff
, 0, SEEK_END
);
721 if (ftell(ff
) > sizeof(hr
->g
.data
)) {
723 "warning: maximum generic record size is %d, rest of file ignored\n",
730 rdfaddheader(rdfheader
, hr
);
735 printf("\nbuilding output module (%d segments)\n", nsegs
);
738 * Allocate the memory for the segments. We may be better off
739 * building the output module one segment at a time when running
740 * under 16 bit DOS, but that would be a slower way of doing this.
741 * And you could always use DJGPP...
743 for (i
= 0; i
< nsegs
; i
++) {
744 outputseg
[i
].data
= NULL
;
745 if (!outputseg
[i
].length
)
747 outputseg
[i
].data
= malloc(outputseg
[i
].length
);
748 if (!outputseg
[i
].data
) {
749 fprintf(stderr
, "ldrdf: out of memory\n");
755 * initialise availableseg, used to allocate segment numbers for
756 * imported and exported labels...
758 availableseg
= nsegs
;
761 * Step through the modules, performing required actions on each one
763 for (cur
= modules
; cur
; cur
= cur
->next
) {
765 * Read the actual segment contents into the correct places in
766 * the newly allocated segments
769 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
770 int dest
= cur
->seginfo
[i
].dest_seg
;
774 if (rdfloadseg(&cur
->f
, i
,
775 outputseg
[dest
].data
+ cur
->seginfo
[i
].reloc
)) {
776 rdfperror("ldrdf", cur
->name
);
782 * Perform fixups, and add new header records where required
785 header
= malloc(cur
->f
.header_len
);
787 fprintf(stderr
, "ldrdf: out of memory\n");
791 if (cur
->f
.header_loc
)
792 rdfheaderrewind(&cur
->f
);
793 else if (rdfloadseg(&cur
->f
, RDOFF_HEADER
, header
)) {
794 rdfperror("ldrdf", cur
->name
);
799 * we need to create a local segment number -> location
800 * table for the segments in this module.
802 init_seglocations(&segs
);
803 for (i
= 0; i
< cur
->f
.nsegs
; i
++) {
804 add_seglocation(&segs
, cur
->f
.seg
[i
].number
,
805 cur
->seginfo
[i
].dest_seg
,
806 cur
->seginfo
[i
].reloc
);
809 * and the BSS segment (doh!)
811 add_seglocation(&segs
, 2, 2, cur
->bss_reloc
);
813 while ((hr
= rdfgetheaderrec(&cur
->f
))) {
815 case RDFREC_RELOC
: /* relocation record - need to do a fixup */
817 * First correct the offset stored in the segment from
818 * the start of the segment (which may well have changed).
820 * To do this we add to the number stored the relocation
821 * factor associated with the segment that contains the
824 * The relocation could be a relative relocation, in which
825 * case we have to first subtract the amount we've relocated
826 * the containing segment by.
828 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
830 "%s: reloc to undefined segment %04x\n",
831 cur
->name
, (int)hr
->r
.refseg
);
837 (hr
->r
.segment
& RDOFF_RELATIVEMASK
) ==
839 hr
->r
.segment
&= (RDOFF_RELATIVEMASK
- 1);
841 if (hr
->r
.segment
== 2 ||
843 rdffindsegment(&cur
->f
, hr
->r
.segment
)) == -1) {
844 fprintf(stderr
, "%s: reloc from %s segment (%d)\n",
846 hr
->r
.segment
== 2 ? "BSS" : "unknown",
852 if (hr
->r
.length
!= 1 && hr
->r
.length
!= 2 &&
854 fprintf(stderr
, "%s: nonstandard length reloc "
855 "(%d bytes)\n", cur
->name
, hr
->r
.length
);
861 * okay, now the relocation is in the segment pointed to by
862 * cur->seginfo[localseg], and we know everything else is
863 * okay to go ahead and do the relocation
865 data
= outputseg
[cur
->seginfo
[localseg
].dest_seg
].data
;
866 data
+= cur
->seginfo
[localseg
].reloc
+ hr
->r
.offset
;
869 * data now points to the reference that needs
870 * relocation. Calculate the relocation factor.
872 * offset of referred object in segment [in offset]
873 * (- relocation of localseg, if ref is relative)
874 * For simplicity, the result is stored in 'offset'.
875 * Then add 'offset' onto the value at data.
879 offset
-= cur
->seginfo
[localseg
].reloc
;
880 switch (hr
->r
.length
) {
883 if (offset
< -127 || offset
> 128)
885 "warning: relocation out of range "
886 "at %s(%02x:%08lx)\n", cur
->name
,
887 (int)hr
->r
.segment
, hr
->r
.offset
);
888 *data
= (char)offset
;
891 offset
+= *(short *)data
;
892 if (offset
< -32767 || offset
> 32768)
894 "warning: relocation out of range "
895 "at %s(%02x:%08lx)\n", cur
->name
,
896 (int)hr
->r
.segment
, hr
->r
.offset
);
897 *(short *)data
= (short)offset
;
900 *(long *)data
+= offset
;
901 /* we can't easily detect overflow on this one */
906 * If the relocation was relative between two symbols in
907 * the same segment, then we're done.
909 * Otherwise, we need to output a new relocation record
910 * with the references updated segment and offset...
912 if (!isrelative
|| cur
->seginfo
[localseg
].dest_seg
!= seg
) {
913 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
914 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
917 hr
->r
.segment
+= RDOFF_RELATIVEMASK
;
918 rdfaddheader(rdfheader
, hr
);
922 case RDFREC_IMPORT
: /* import symbol */
923 case RDFREC_FARIMPORT
:
925 * scan the global symbol table for the symbol
926 * and associate its location with the segment number
929 se
= symtabFind(symtab
, hr
->i
.label
);
930 if (!se
|| se
->segment
== -1) {
931 if (!options
.dynalink
&& !(hr
->i
.flags
& SYM_IMPORT
)) {
933 "error: unresolved reference to `%s'"
934 " in module `%s'\n", hr
->i
.label
,
939 * we need to allocate a segment number for this
940 * symbol, and store it in the symbol table for
944 se
= malloc(sizeof(*se
));
946 fprintf(stderr
, "ldrdf: out of memory\n");
949 se
->name
= strdup(hr
->i
.label
);
951 se
->segment
= availableseg
++;
953 symtabInsert(symtab
, se
);
955 se
->segment
= availableseg
++;
959 * output a header record that imports it to the
960 * recently allocated segment number...
963 newrec
.i
.segment
= se
->segment
;
964 rdfaddheader(rdfheader
, &newrec
);
967 add_seglocation(&segs
, hr
->i
.segment
, se
->segment
,
971 case RDFREC_GLOBAL
: /* export symbol */
973 * need to insert an export for this symbol into the new
974 * header, unless we're stripping symbols. Even if we're
975 * stripping, put the symbol if it's marked as SYM_GLOBAL.
977 if (options
.strip
&& !(hr
->e
.flags
& SYM_GLOBAL
))
980 if (hr
->e
.segment
== 2) {
982 offset
= cur
->bss_reloc
;
984 localseg
= rdffindsegment(&cur
->f
, hr
->e
.segment
);
985 if (localseg
== -1) {
986 fprintf(stderr
, "%s: exported symbol `%s' from "
987 "unrecognised segment\n", cur
->name
,
992 offset
= cur
->seginfo
[localseg
].reloc
;
993 seg
= cur
->seginfo
[localseg
].dest_seg
;
997 hr
->e
.offset
+= offset
;
998 rdfaddheader(rdfheader
, hr
);
1001 case RDFREC_MODNAME
: /* module name */
1003 * Insert module name record if export symbols
1005 * If module name begins with '$' - insert it anyway.
1007 if (options
.strip
&& hr
->m
.modname
[0] != '$')
1009 rdfaddheader(rdfheader
, hr
);
1012 case RDFREC_DLL
: /* DLL name */
1014 * Insert DLL name if it begins with '$'
1016 if (hr
->d
.libname
[0] != '$')
1018 rdfaddheader(rdfheader
, hr
);
1021 case RDFREC_SEGRELOC
: /* segment fixup */
1023 * modify the segment numbers if necessary, and
1024 * pass straight through to the output module header
1028 if (hr
->r
.segment
== 2) {
1029 fprintf(stderr
, "%s: segment fixup in BSS section\n",
1034 localseg
= rdffindsegment(&cur
->f
, hr
->r
.segment
);
1035 if (localseg
== -1) {
1036 fprintf(stderr
, "%s: segment fixup in unrecognised"
1037 " segment (%d)\n", cur
->name
, hr
->r
.segment
);
1041 hr
->r
.segment
= cur
->seginfo
[localseg
].dest_seg
;
1042 hr
->r
.offset
+= cur
->seginfo
[localseg
].reloc
;
1044 if (!get_seglocation(&segs
, hr
->r
.refseg
, &seg
, &offset
)) {
1045 fprintf(stderr
, "%s: segment fixup to undefined "
1046 "segment %04x\n", cur
->name
,
1052 rdfaddheader(rdfheader
, hr
);
1055 case RDFREC_COMMON
: /* Common variable */
1056 /* Is this symbol already in the table? */
1057 se
= symtabFind(symtab
, hr
->c
.label
);
1059 printf("%s is not in symtab yet\n", hr
->c
.label
);
1062 /* Add segment location */
1063 add_seglocation(&segs
, hr
->c
.segment
, se
->segment
,
1070 done_seglocations(&segs
);
1075 * combined BSS reservation for the entire results
1077 newrec
.type
= RDFREC_BSS
;
1078 newrec
.b
.reclen
= 4;
1079 newrec
.b
.amount
= bss_length
;
1080 rdfaddheader(rdfheader
, &newrec
);
1085 for (i
= 0; i
< nsegs
; i
++) {
1088 rdfaddsegment(rdfheader
, outputseg
[i
].length
);
1091 rdfwriteheader(f
, rdfheader
);
1092 rdfdoneheader(rdfheader
);
1095 * Step through the segments, one at a time, writing out into
1098 for (i
= 0; i
< nsegs
; i
++) {
1105 s
= translateshort(outputseg
[i
].type
);
1106 fwrite(&s
, 2, 1, f
);
1107 s
= translateshort(outputseg
[i
].number
);
1108 fwrite(&s
, 2, 1, f
);
1109 s
= translateshort(outputseg
[i
].reserved
);
1110 fwrite(&s
, 2, 1, f
);
1111 l
= translatelong(outputseg
[i
].length
);
1112 fwrite(&l
, 4, 1, f
);
1114 fwrite(outputseg
[i
].data
, outputseg
[i
].length
, 1, f
);
1117 fwrite("\0\0\0\0\0\0\0\0\0\0", 10, 1, f
);
1120 /* =========================================================================
1127 " ldrdf [options] object modules ... [-llibrary ...]\n"
1130 " -v[=n] increase verbosity by 1, or set it to n\n"
1131 " -a nn set segment alignment value (default 16)\n"
1132 " -s strip public symbols\n"
1133 " -dy Unix-style dynamic linking\n"
1134 " -o name write output in file 'name'\n"
1135 " -j path specify objects search path\n"
1136 " -L path specify libraries search path\n"
1137 " -g file embed 'file' as a first header record with type 'generic'\n");
1141 int main(int argc
, char **argv
)
1143 char *outname
= "aout.rdf";
1144 int moduleloaded
= 0;
1145 char *respstrings
[128] = { 0, };
1147 options
.verbose
= 0;
1149 options
.dynalink
= 0;
1152 error_file
= stderr
;
1157 while (argc
&& *argv
&& **argv
== '-' && argv
[0][1] != 'l') {
1158 switch (argv
[0][1]) {
1160 printf("ldrdf (linker for RDF files) version " LDRDF_VERSION
1162 printf("RDOFF2 revision %s\n", RDOFF2_REVISION
);
1165 if (argv
[0][2] == '=') {
1166 options
.verbose
= argv
[0][3] - '0';
1167 if (options
.verbose
< 0 || options
.verbose
> 9) {
1169 "ldrdf: verbosity level must be a number"
1170 " between 0 and 9\n");
1177 options
.align
= atoi(argv
[1]);
1178 if (options
.align
<= 0) {
1180 "ldrdf: -a expects a positive number argument\n");
1189 if (argv
[0][2] == 'y')
1190 options
.dynalink
= 1;
1198 options
.objpath
= 1;
1204 "ldrdf: more than one objects search path specified\n");
1209 options
.libpath
= 1;
1215 "ldrdf: more than one libraries search path specified\n");
1223 options
.respfile
= 1;
1224 if (argv
[1] != NULL
)
1225 f
= fopen(argv
[1], "r");
1228 "ldrdf: no response file name specified\n");
1234 "ldrdf: unable to open response file\n");
1239 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
1243 if ((p
= strchr(buf
, '\n')) != NULL
)
1246 fprintf(stderr
, "ldrdf: too many input files\n");
1249 *(respstrings
+ i
) = newstr(buf
);
1255 options
.stderr_redir
= 1;
1256 error_file
= stdout
;
1259 generic_rec_file
= argv
[1];
1268 if (options
.verbose
> 4) {
1269 printf("ldrdf invoked with options:\n");
1270 printf(" section alignment: %d bytes\n", options
.align
);
1271 printf(" output name: `%s'\n", outname
);
1273 printf(" strip symbols\n");
1274 if (options
.dynalink
)
1275 printf(" Unix-style dynamic linking\n");
1276 if (options
.objpath
)
1277 printf(" objects search path: %s\n", objpath
);
1278 if (options
.libpath
)
1279 printf(" libraries search path: %s\n", libpath
);
1283 symtab
= symtabNew();
1287 fprintf(stderr
, "ldrdf: out of memory\n");
1296 if (!strncmp(*argv
, "-l", 2)) {
1297 if (libpath
&& (argv
[0][2] != '/'))
1298 add_library(newstrcat(libpath
, *argv
+ 2));
1300 add_library(*argv
+ 2);
1302 if (objpath
&& (argv
[0][0] != '/'))
1303 loadmodule(newstrcat(objpath
, *argv
));
1311 if (!moduleloaded
) {
1312 printf("ldrdf: nothing to do. ldrdf -h for usage\n");
1318 if (options
.verbose
> 2) {
1319 printf("symbol table:\n");
1320 symtabDump(symtab
, stdout
);
1323 write_output(outname
);