1 /* outelf.c output routines for the Netwide Assembler to produce
2 * ELF32 (i386 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
24 #define R_386_32 1 /* ordinary absolute relocation */
25 #define R_386_PC32 2 /* PC-relative relocation */
26 #define R_386_GOT32 3 /* an offset into GOT */
27 #define R_386_PLT32 4 /* a PC-relative offset into PLT */
28 #define R_386_GOTOFF 9 /* an offset from GOT base */
29 #define R_386_GOTPC 10 /* a PC-relative offset _to_ GOT */
33 long address
; /* relative to _start_ of section */
34 long symbol
; /* ELF symbol info thingy */
35 int type
; /* type of relocation */
39 long strpos
; /* string table position of name */
40 long section
; /* section ID of the symbol */
41 int type
; /* symbol type */
42 long value
; /* address, or COMMON variable align */
43 long size
; /* size of symbol */
44 long globnum
; /* symbol table offset if global */
45 struct Symbol
*next
; /* list of globals in each section */
46 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
47 char *name
; /* used temporarily if in above list */
50 #define SHT_PROGBITS 1
55 #define SHF_EXECINSTR 4
59 unsigned long len
, size
, nrelocs
;
61 int type
; /* SHT_PROGBITS or SHT_NOBITS */
62 int align
; /* alignment: power of two */
63 unsigned long flags
; /* section flags */
67 struct Reloc
*head
, **tail
;
68 struct Symbol
*gsyms
; /* global symbols in section */
72 static struct Section
**sects
;
73 static int nsects
, sectlen
;
75 #define SHSTR_DELTA 256
76 static char *shstrtab
;
77 static int shstrtablen
, shstrtabsize
;
79 static struct SAA
*syms
;
80 static unsigned long nlocals
, nglobs
;
84 static struct RAA
*bsym
;
86 static struct SAA
*strs
;
87 static unsigned long strslen
;
91 static evalfunc evaluate
;
93 static struct Symbol
*fwds
;
95 static char elf_module
[FILENAME_MAX
];
97 extern struct ofmt of_elf
;
99 #define SHN_ABS 0xFFF1
100 #define SHN_COMMON 0xFFF2
103 #define SYM_SECTION 0x04
104 #define SYM_GLOBAL 0x10
105 #define SYM_DATA 0x01
106 #define SYM_FUNCTION 0x02
108 #define GLOBAL_TEMP_BASE 6 /* bigger than any constant sym id */
110 #define SEG_ALIGN 16 /* alignment of sections in file */
111 #define SEG_ALIGN_1 (SEG_ALIGN-1)
113 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
115 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
116 static struct ELF_SECTDATA
{
120 } elf_sects
[ELF_MAX_SECTIONS
];
121 static int elf_nsect
;
122 static long elf_foffs
;
124 static void elf_write(void);
125 static void elf_sect_write(struct Section
*, unsigned char *, unsigned long);
126 static void elf_section_header (int, int, int, void *, int, long,
128 static void elf_write_sections (void);
129 static struct SAA
*elf_build_symtab (long *, long *);
130 static struct SAA
*elf_build_reltab (long *, struct Reloc
*);
131 static void add_sectname (char *, char *);
134 * Special section numbers which are used to define ELF special
135 * symbols, which can be used with WRT to provide PIC relocation
138 static long elf_gotpc_sect
, elf_gotoff_sect
;
139 static long elf_got_sect
, elf_plt_sect
;
140 static long elf_sym_sect
;
142 static void elf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
) {
146 (void) ldef
; /* placate optimisers */
148 nsects
= sectlen
= 0;
149 syms
= saa_init((long)sizeof(struct Symbol
));
150 nlocals
= nglobs
= 0;
153 saa_wbytes (strs
, "\0", 1L);
154 saa_wbytes (strs
, elf_module
, (long)(strlen(elf_module
)+1));
155 strslen
= 2+strlen(elf_module
);
157 shstrtablen
= shstrtabsize
= 0;;
158 add_sectname ("", "");
162 elf_gotpc_sect
= seg_alloc();
163 ldef("..gotpc", elf_gotpc_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
164 elf_gotoff_sect
= seg_alloc();
165 ldef("..gotoff", elf_gotoff_sect
+1, 0L, NULL
, FALSE
, FALSE
,&of_elf
,error
);
166 elf_got_sect
= seg_alloc();
167 ldef("..got", elf_got_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
168 elf_plt_sect
= seg_alloc();
169 ldef("..plt", elf_plt_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
170 elf_sym_sect
= seg_alloc();
171 ldef("..sym", elf_sym_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
173 def_seg
= seg_alloc();
176 static void elf_cleanup(void) {
182 for (i
=0; i
<nsects
; i
++) {
183 if (sects
[i
]->type
!= SHT_NOBITS
)
184 saa_free (sects
[i
]->data
);
186 saa_free (sects
[i
]->rel
);
187 while (sects
[i
]->head
) {
189 sects
[i
]->head
= sects
[i
]->head
->next
;
199 static void add_sectname (char *firsthalf
, char *secondhalf
) {
200 int len
= strlen(firsthalf
)+strlen(secondhalf
);
201 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
202 shstrtab
= nasm_realloc (shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
203 strcpy (shstrtab
+shstrtablen
, firsthalf
);
204 strcat (shstrtab
+shstrtablen
, secondhalf
);
205 shstrtablen
+= len
+1;
208 static int elf_make_section (char *name
, int type
, int flags
, int align
) {
211 s
= nasm_malloc (sizeof(*s
));
213 if (type
!= SHT_NOBITS
)
214 s
->data
= saa_init (1L);
217 s
->len
= s
->size
= 0;
219 if (!strcmp(name
, ".text"))
222 s
->index
= seg_alloc();
223 add_sectname ("", name
);
224 s
->name
= nasm_malloc (1+strlen(name
));
225 strcpy (s
->name
, name
);
231 if (nsects
>= sectlen
)
232 sects
= nasm_realloc (sects
, (sectlen
+= SECT_DELTA
)*sizeof(*sects
));
238 static long elf_section_names (char *name
, int pass
, int *bits
) {
240 int flags_and
, flags_or
, type
, align
, i
;
243 * Default is 32 bits.
252 while (*p
&& !isspace(*p
)) p
++;
254 flags_and
= flags_or
= type
= align
= 0;
256 while (*p
&& isspace(*p
)) p
++;
259 while (*p
&& !isspace(*p
)) p
++;
261 while (*p
&& isspace(*p
)) p
++;
263 if (!nasm_strnicmp(q
, "align=", 6)) {
267 if ( (align
-1) & align
) { /* means it's not a power of two */
268 error (ERR_NONFATAL
, "section alignment %d is not"
269 " a power of two", align
);
272 } else if (!nasm_stricmp(q
, "alloc")) {
273 flags_and
|= SHF_ALLOC
;
274 flags_or
|= SHF_ALLOC
;
275 } else if (!nasm_stricmp(q
, "noalloc")) {
276 flags_and
|= SHF_ALLOC
;
277 flags_or
&= ~SHF_ALLOC
;
278 } else if (!nasm_stricmp(q
, "exec")) {
279 flags_and
|= SHF_EXECINSTR
;
280 flags_or
|= SHF_EXECINSTR
;
281 } else if (!nasm_stricmp(q
, "noexec")) {
282 flags_and
|= SHF_EXECINSTR
;
283 flags_or
&= ~SHF_EXECINSTR
;
284 } else if (!nasm_stricmp(q
, "write")) {
285 flags_and
|= SHF_WRITE
;
286 flags_or
|= SHF_WRITE
;
287 } else if (!nasm_stricmp(q
, "nowrite")) {
288 flags_and
|= SHF_WRITE
;
289 flags_or
&= ~SHF_WRITE
;
290 } else if (!nasm_stricmp(q
, "progbits")) {
292 } else if (!nasm_stricmp(q
, "nobits")) {
297 if (!strcmp(name
, ".comment") ||
298 !strcmp(name
, ".shstrtab") ||
299 !strcmp(name
, ".symtab") ||
300 !strcmp(name
, ".strtab")) {
301 error (ERR_NONFATAL
, "attempt to redefine reserved section"
306 for (i
=0; i
<nsects
; i
++)
307 if (!strcmp(name
, sects
[i
]->name
))
310 if (!strcmp(name
, ".text"))
311 i
= elf_make_section (name
, SHT_PROGBITS
,
312 SHF_ALLOC
| SHF_EXECINSTR
, 16);
313 else if (!strcmp(name
, ".data"))
314 i
= elf_make_section (name
, SHT_PROGBITS
,
315 SHF_ALLOC
| SHF_WRITE
, 4);
316 else if (!strcmp(name
, ".bss"))
317 i
= elf_make_section (name
, SHT_NOBITS
,
318 SHF_ALLOC
| SHF_WRITE
, 4);
320 i
= elf_make_section (name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
322 sects
[i
]->type
= type
;
324 sects
[i
]->align
= align
;
325 sects
[i
]->flags
&= ~flags_and
;
326 sects
[i
]->flags
|= flags_or
;
327 } else if (pass
== 1) {
328 if (type
|| align
|| flags_and
)
329 error (ERR_WARNING
, "section attributes ignored on"
330 " redeclaration of section `%s'", name
);
333 return sects
[i
]->index
;
336 static void elf_deflabel (char *name
, long segment
, long offset
,
337 int is_global
, char *special
) {
340 int special_used
= FALSE
;
342 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
344 * This is a NASM special symbol. We never allow it into
345 * the ELF symbol table, even if it's a valid one. If it
346 * _isn't_ a valid one, we should barf immediately.
348 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
349 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
350 strcmp(name
, "..sym"))
351 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
355 if (is_global
== 3) {
358 * Fix up a forward-reference symbol size from the first
361 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
362 if (!strcmp((*s
)->name
, name
)) {
363 struct tokenval tokval
;
367 while (*p
&& !isspace(*p
)) p
++;
368 while (*p
&& isspace(*p
)) p
++;
371 tokval
.t_type
= TOKEN_INVALID
;
372 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
375 error (ERR_NONFATAL
, "cannot use relocatable"
376 " expression as symbol size");
378 (*s
)->size
= reloc_value(e
);
382 * Remove it from the list of unresolved sizes.
384 nasm_free ((*s
)->name
);
388 return; /* it wasn't an important one */
391 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
392 strslen
+= 1+strlen(name
);
394 sym
= saa_wstruct (syms
);
397 sym
->type
= is_global
? SYM_GLOBAL
: 0;
399 if (segment
== NO_SEG
)
400 sym
->section
= SHN_ABS
;
403 sym
->section
= SHN_UNDEF
;
404 if (nsects
== 0 && segment
== def_seg
) {
406 if (segment
!= elf_section_names (".text", 2, &tempint
))
407 error (ERR_PANIC
, "strange segment conditions in ELF driver");
408 sym
->section
= nsects
;
410 for (i
=0; i
<nsects
; i
++)
411 if (segment
== sects
[i
]->index
) {
418 if (is_global
== 2) {
421 sym
->section
= SHN_COMMON
;
423 * We have a common variable. Check the special text to see
424 * if it's a valid number and power of two; if so, store it
425 * as the alignment for the common variable.
429 sym
->value
= readnum (special
, &err
);
431 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
432 " valid number", special
);
433 else if ( (sym
->value
| (sym
->value
-1)) != 2*sym
->value
- 1)
434 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
435 " power of two", special
);
439 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
441 if (sym
->type
== SYM_GLOBAL
) {
442 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
443 bsym
= raa_write (bsym
, segment
, nglobs
);
446 * This is a global symbol; so we must add it to the linked
447 * list of global symbols in its section. We'll push it on
448 * the beginning of the list, because it doesn't matter
449 * much which end we put it on and it's easier like this.
451 * In addition, we check the special text for symbol
452 * type and size information.
454 sym
->next
= sects
[sym
->section
-1]->gsyms
;
455 sects
[sym
->section
-1]->gsyms
= sym
;
458 int n
= strcspn(special
, " ");
460 if (!nasm_strnicmp(special
, "function", n
))
461 sym
->type
|= SYM_FUNCTION
;
462 else if (!nasm_strnicmp(special
, "data", n
) ||
463 !nasm_strnicmp(special
, "object", n
))
464 sym
->type
|= SYM_DATA
;
466 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
469 struct tokenval tokval
;
473 while (special
[n
] && isspace(special
[n
]))
476 * We have a size expression; attempt to
480 stdscan_bufptr
= special
+n
;
481 tokval
.t_type
= TOKEN_INVALID
;
482 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
, NULL
);
486 sym
->name
= nasm_strdup(name
);
489 error (ERR_NONFATAL
, "cannot use relocatable"
490 " expression as symbol size");
492 sym
->size
= reloc_value(e
);
498 sym
->globnum
= nglobs
;
503 if (special
&& !special_used
)
504 error(ERR_NONFATAL
, "no special symbol features supported here");
507 static void elf_add_reloc (struct Section
*sect
, long segment
,
511 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
512 sect
->tail
= &r
->next
;
515 r
->address
= sect
->len
;
516 if (segment
== NO_SEG
)
521 for (i
=0; i
<nsects
; i
++)
522 if (segment
== sects
[i
]->index
)
525 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
533 * This routine deals with ..got and ..sym relocations: the more
534 * complicated kinds. In shared-library writing, some relocations
535 * with respect to global symbols must refer to the precise symbol
536 * rather than referring to an offset from the base of the section
537 * _containing_ the symbol. Such relocations call to this routine,
538 * which searches the symbol list for the symbol in question.
540 * R_386_GOT32 references require the _exact_ symbol address to be
541 * used; R_386_32 references can be at an offset from the symbol.
542 * The boolean argument `exact' tells us this.
544 * Return value is the adjusted value of `addr', having become an
545 * offset from the symbol rather than the section. Should always be
546 * zero when returning from an exact call.
548 * Limitation: if you define two symbols at the same place,
549 * confusion will occur.
551 * Inefficiency: we search, currently, using a linked list which
552 * isn't even necessarily sorted.
554 static long elf_add_gsym_reloc (struct Section
*sect
,
555 long segment
, long offset
,
556 int type
, int exact
) {
559 struct Symbol
*sym
, *sm
;
563 * First look up the segment/offset pair and find a global
564 * symbol corresponding to it. If it's not one of our segments,
565 * then it must be an external symbol, in which case we're fine
566 * doing a normal elf_add_reloc after first sanity-checking
567 * that the offset from the symbol is zero.
570 for (i
=0; i
<nsects
; i
++)
571 if (segment
== sects
[i
]->index
) {
576 if (exact
&& offset
!= 0)
577 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
578 " for this reference");
580 elf_add_reloc (sect
, segment
, type
);
586 * Find a symbol pointing _exactly_ at this one.
588 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
589 if (sym
->value
== offset
)
593 * Find the nearest symbol below this one.
596 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
597 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
601 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
602 " for this reference");
606 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
607 sect
->tail
= &r
->next
;
610 r
->address
= sect
->len
;
611 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
616 return offset
- sym
->value
;
619 static void elf_out (long segto
, void *data
, unsigned long type
,
620 long segment
, long wrt
) {
622 long realbytes
= type
& OUT_SIZMASK
;
624 unsigned char mydata
[4], *p
;
630 * handle absolute-assembly (structure definitions)
632 if (segto
== NO_SEG
) {
633 if (type
!= OUT_RESERVE
)
634 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
640 for (i
=0; i
<nsects
; i
++)
641 if (segto
== sects
[i
]->index
) {
646 int tempint
; /* ignored */
647 if (segto
!= elf_section_names (".text", 2, &tempint
))
648 error (ERR_PANIC
, "strange segment conditions in ELF driver");
653 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
654 error(ERR_WARNING
, "attempt to initialise memory in"
655 " BSS section `%s': ignored", s
->name
);
656 if (type
== OUT_REL2ADR
)
658 else if (type
== OUT_REL4ADR
)
664 if (type
== OUT_RESERVE
) {
665 if (s
->type
== SHT_PROGBITS
) {
666 error(ERR_WARNING
, "uninitialised space declared in"
667 " non-BSS section `%s': zeroing", s
->name
);
668 elf_sect_write (s
, NULL
, realbytes
);
671 } else if (type
== OUT_RAWDATA
) {
672 if (segment
!= NO_SEG
)
673 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
674 elf_sect_write (s
, data
, realbytes
);
675 } else if (type
== OUT_ADDRESS
) {
676 addr
= *(long *)data
;
677 if (segment
!= NO_SEG
) {
679 error(ERR_NONFATAL
, "ELF format does not support"
680 " segment base references");
683 elf_add_reloc (s
, segment
, R_386_32
);
684 } else if (wrt
== elf_gotpc_sect
+1) {
686 * The user will supply GOT relative to $$. ELF
687 * will let us have GOT relative to $. So we
688 * need to fix up the data item by $-$$.
691 elf_add_reloc (s
, segment
, R_386_GOTPC
);
692 } else if (wrt
== elf_gotoff_sect
+1) {
693 elf_add_reloc (s
, segment
, R_386_GOTOFF
);
694 } else if (wrt
== elf_got_sect
+1) {
695 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
697 } else if (wrt
== elf_sym_sect
+1) {
698 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
700 } else if (wrt
== elf_plt_sect
+1) {
701 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
702 "relative PLT references");
704 error (ERR_NONFATAL
, "ELF format does not support this"
706 wrt
= NO_SEG
; /* we can at least _try_ to continue */
711 if (realbytes
!= 4 && segment
!= NO_SEG
)
712 error (ERR_NONFATAL
, "ELF format does not support non-32-bit"
715 elf_sect_write (s
, mydata
, realbytes
);
716 } else if (type
== OUT_REL2ADR
) {
717 error (ERR_NONFATAL
, "ELF format does not support 16-bit"
719 } else if (type
== OUT_REL4ADR
) {
720 if (segment
== segto
)
721 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
722 if (segment
!= NO_SEG
&& segment
% 2) {
723 error(ERR_NONFATAL
, "ELF format does not support"
724 " segment base references");
727 elf_add_reloc (s
, segment
, R_386_PC32
);
728 } else if (wrt
== elf_plt_sect
+1) {
729 elf_add_reloc (s
, segment
, R_386_PLT32
);
730 } else if (wrt
== elf_gotpc_sect
+1 ||
731 wrt
== elf_gotoff_sect
+1 ||
732 wrt
== elf_got_sect
+1) {
733 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
734 "relative GOT references");
736 error (ERR_NONFATAL
, "ELF format does not support this"
738 wrt
= NO_SEG
; /* we can at least _try_ to continue */
742 WRITELONG (p
, *(long*)data
- realbytes
);
743 elf_sect_write (s
, mydata
, 4L);
747 static void elf_write(void) {
748 int nsections
, align
;
755 long symtablen
, symtablocal
;
758 * Work out how many sections we will have. We have SHN_UNDEF,
759 * then the flexible user sections, then the four fixed
760 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
761 * then optionally relocation sections for the user sections.
763 nsections
= 5; /* SHN_UNDEF and the fixed ones */
764 add_sectname ("", ".comment");
765 add_sectname ("", ".shstrtab");
766 add_sectname ("", ".symtab");
767 add_sectname ("", ".strtab");
768 for (i
=0; i
<nsects
; i
++) {
769 nsections
++; /* for the section itself */
770 if (sects
[i
]->head
) {
771 nsections
++; /* for its relocations */
772 add_sectname (".rel", sects
[i
]->name
);
780 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
783 * Output the ELF header.
785 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
786 fwriteshort (1, elffp
); /* ET_REL relocatable file */
787 fwriteshort (3, elffp
); /* EM_386 processor ID */
788 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
789 fwritelong (0L, elffp
); /* no entry point */
790 fwritelong (0L, elffp
); /* no program header table */
791 fwritelong (0x40L
, elffp
); /* section headers straight after
792 * ELF header plus alignment */
793 fwritelong (0L, elffp
); /* 386 defines no special flags */
794 fwriteshort (0x34, elffp
); /* size of ELF header */
795 fwriteshort (0, elffp
); /* no program header table, again */
796 fwriteshort (0, elffp
); /* still no program header table */
797 fwriteshort (0x28, elffp
); /* size of section header */
798 fwriteshort (nsections
, elffp
); /* number of sections */
799 fwriteshort (nsects
+2, elffp
); /* string table section index for
800 * section header table */
801 fwritelong (0L, elffp
); /* align to 0x40 bytes */
802 fwritelong (0L, elffp
);
803 fwritelong (0L, elffp
);
806 * Build the symbol table and relocation tables.
808 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
809 for (i
=0; i
<nsects
; i
++)
811 sects
[i
]->rel
= elf_build_reltab (§s
[i
]->rellen
,
815 * Now output the section header table.
818 elf_foffs
= 0x40 + 0x28 * nsections
;
819 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
823 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
825 for (i
=0; i
<nsects
; i
++) {
826 elf_section_header (p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
827 (sects
[i
]->type
== SHT_PROGBITS
?
828 sects
[i
]->data
: NULL
), TRUE
,
829 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
832 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
833 (long)commlen
, 0, 0, 1, 0);/* .comment */
835 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
836 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
838 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
839 symtablen
, nsects
+4, symtablocal
, 4, 16);/* .symtab */
841 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
842 strslen
, 0, 0, 1, 0); /* .strtab */
843 for (i
=0; i
<nsects
; i
++) if (sects
[i
]->head
) {
845 elf_section_header (p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
846 sects
[i
]->rellen
, nsects
+3, i
+1, 4, 8);
849 fwrite (align_str
, align
, 1, elffp
);
852 * Now output the sections.
854 elf_write_sections();
859 static struct SAA
*elf_build_symtab (long *len
, long *local
) {
860 struct SAA
*s
= saa_init(1L);
862 unsigned char entry
[16], *p
;
868 * First, an all-zeros entry, required by the ELF spec.
870 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
875 * Next, an entry for the file name.
878 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
879 WRITELONG (p
, 0); /* no value */
880 WRITELONG (p
, 0); /* no size either */
881 WRITESHORT (p
, 4); /* type FILE */
882 WRITESHORT (p
, SHN_ABS
);
883 saa_wbytes (s
, entry
, 16L);
888 * Now some standard symbols defining the segments, for relocation
891 for (i
= 1; i
<= nsects
+1; i
++) {
893 WRITELONG (p
, 0); /* no symbol name */
894 WRITELONG (p
, 0); /* offset zero */
895 WRITELONG (p
, 0); /* size zero */
896 WRITESHORT (p
, 3); /* local section-type thing */
897 WRITESHORT (p
, (i
==1 ? SHN_ABS
: i
-1)); /* the section id */
898 saa_wbytes (s
, entry
, 16L);
904 * Now the other local symbols.
907 while ( (sym
= saa_rstruct (syms
)) ) {
908 if (sym
->type
& SYM_GLOBAL
)
911 WRITELONG (p
, sym
->strpos
);
912 WRITELONG (p
, sym
->value
);
913 WRITELONG (p
, sym
->size
);
914 WRITESHORT (p
, sym
->type
); /* local non-typed thing */
915 WRITESHORT (p
, sym
->section
);
916 saa_wbytes (s
, entry
, 16L);
922 * Now the global symbols.
925 while ( (sym
= saa_rstruct (syms
)) ) {
926 if (!(sym
->type
& SYM_GLOBAL
))
929 WRITELONG (p
, sym
->strpos
);
930 WRITELONG (p
, sym
->value
);
931 WRITELONG (p
, sym
->size
);
932 WRITESHORT (p
, sym
->type
); /* global non-typed thing */
933 WRITESHORT (p
, sym
->section
);
934 saa_wbytes (s
, entry
, 16L);
941 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
943 unsigned char *p
, entry
[8];
952 long sym
= r
->symbol
;
954 if (sym
>= GLOBAL_TEMP_BASE
)
955 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+3) + nlocals
;
958 WRITELONG (p
, r
->address
);
959 WRITELONG (p
, (sym
<< 8) + r
->type
);
960 saa_wbytes (s
, entry
, 8L);
969 static void elf_section_header (int name
, int type
, int flags
,
970 void *data
, int is_saa
, long datalen
,
971 int link
, int info
, int align
, int eltsize
) {
972 elf_sects
[elf_nsect
].data
= data
;
973 elf_sects
[elf_nsect
].len
= datalen
;
974 elf_sects
[elf_nsect
].is_saa
= is_saa
;
977 fwritelong ((long)name
, elffp
);
978 fwritelong ((long)type
, elffp
);
979 fwritelong ((long)flags
, elffp
);
980 fwritelong (0L, elffp
); /* no address, ever, in object files */
981 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
982 fwritelong (datalen
, elffp
);
984 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
985 fwritelong ((long)link
, elffp
);
986 fwritelong ((long)info
, elffp
);
987 fwritelong ((long)align
, elffp
);
988 fwritelong ((long)eltsize
, elffp
);
991 static void elf_write_sections (void) {
993 for (i
= 0; i
< elf_nsect
; i
++)
994 if (elf_sects
[i
].data
) {
995 long len
= elf_sects
[i
].len
;
996 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
997 long align
= reallen
- len
;
998 if (elf_sects
[i
].is_saa
)
999 saa_fpwrite (elf_sects
[i
].data
, elffp
);
1001 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
1002 fwrite (align_str
, align
, 1, elffp
);
1006 static void elf_sect_write (struct Section
*sect
,
1007 unsigned char *data
, unsigned long len
) {
1008 saa_wbytes (sect
->data
, data
, len
);
1012 static long elf_segbase (long segment
) {
1016 static int elf_directive (char *directive
, char *value
, int pass
) {
1020 static void elf_filename (char *inname
, char *outname
, efunc error
) {
1021 strcpy(elf_module
, inname
);
1022 standard_extension (inname
, outname
, ".o", error
);
1025 static char *elf_stdmac
[] = {
1026 "%define __SECT__ [section .text]",
1030 struct ofmt of_elf
= {
1031 "ELF32 (i386) object files (e.g. Linux)",