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.
25 R_386_32
= 1, /* ordinary absolute relocation */
26 R_386_PC32
= 2, /* PC-relative relocation */
27 R_386_GOT32
= 3, /* an offset into GOT */
28 R_386_PLT32
= 4, /* a PC-relative offset into PLT */
29 R_386_COPY
= 5, /* ??? */
30 R_386_GLOB_DAT
= 6, /* ??? */
31 R_386_JUMP_SLOT
= 7, /* ??? */
32 R_386_RELATIVE
= 8, /* ??? */
33 R_386_GOTOFF
= 9, /* an offset from GOT base */
34 R_386_GOTPC
= 10, /* a PC-relative offset _to_ GOT */
35 /* These are GNU extensions, but useful */
36 R_386_16
= 20, /* A 16-bit absolute relocation */
37 R_386_PC16
= 21, /* A 16-bit PC-relative relocation */
38 R_386_8
= 22, /* An 8-bit absolute relocation */
39 R_386_PC8
= 23 /* An 8-bit PC-relative relocation */
44 long address
; /* relative to _start_ of section */
45 long symbol
; /* ELF symbol info thingy */
46 int type
; /* type of relocation */
50 long strpos
; /* string table position of name */
51 long section
; /* section ID of the symbol */
52 int type
; /* symbol type */
53 long value
; /* address, or COMMON variable align */
54 long size
; /* size of symbol */
55 long globnum
; /* symbol table offset if global */
56 struct Symbol
*next
; /* list of globals in each section */
57 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
58 char *name
; /* used temporarily if in above list */
61 #define SHT_PROGBITS 1
66 #define SHF_EXECINSTR 4
70 unsigned long len
, size
, nrelocs
;
72 int type
; /* SHT_PROGBITS or SHT_NOBITS */
73 int align
; /* alignment: power of two */
74 unsigned long flags
; /* section flags */
78 struct Reloc
*head
, **tail
;
79 struct Symbol
*gsyms
; /* global symbols in section */
83 static struct Section
**sects
;
84 static int nsects
, sectlen
;
86 #define SHSTR_DELTA 256
87 static char *shstrtab
;
88 static int shstrtablen
, shstrtabsize
;
90 static struct SAA
*syms
;
91 static unsigned long nlocals
, nglobs
;
95 static struct RAA
*bsym
;
97 static struct SAA
*strs
;
98 static unsigned long strslen
;
102 static evalfunc evaluate
;
104 static struct Symbol
*fwds
;
106 static char elf_module
[FILENAME_MAX
];
108 extern struct ofmt of_elf
;
110 #define SHN_ABS 0xFFF1
111 #define SHN_COMMON 0xFFF2
114 #define SYM_SECTION 0x04
115 #define SYM_GLOBAL 0x10
116 #define SYM_DATA 0x01
117 #define SYM_FUNCTION 0x02
119 #define GLOBAL_TEMP_BASE 15 /* bigger than any constant sym id */
121 #define SEG_ALIGN 16 /* alignment of sections in file */
122 #define SEG_ALIGN_1 (SEG_ALIGN-1)
124 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
126 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
127 static struct ELF_SECTDATA
{
132 static int elf_nsect
;
133 static long elf_foffs
;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section
*, unsigned char *, unsigned long);
137 static void elf_section_header (int, int, int, void *, int, long,
139 static void elf_write_sections (void);
140 static struct SAA
*elf_build_symtab (long *, long *);
141 static struct SAA
*elf_build_reltab (long *, struct Reloc
*);
142 static void add_sectname (char *, char *);
145 * Special section numbers which are used to define ELF special
146 * symbols, which can be used with WRT to provide PIC relocation
149 static long elf_gotpc_sect
, elf_gotoff_sect
;
150 static long elf_got_sect
, elf_plt_sect
;
151 static long elf_sym_sect
;
153 static void elf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
158 (void) ldef
; /* placate optimisers */
160 nsects
= sectlen
= 0;
161 syms
= saa_init((long)sizeof(struct Symbol
));
162 nlocals
= nglobs
= 0;
165 saa_wbytes (strs
, "\0", 1L);
166 saa_wbytes (strs
, elf_module
, (long)(strlen(elf_module
)+1));
167 strslen
= 2+strlen(elf_module
);
169 shstrtablen
= shstrtabsize
= 0;;
170 add_sectname ("", "");
174 elf_gotpc_sect
= seg_alloc();
175 ldef("..gotpc", elf_gotpc_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
176 elf_gotoff_sect
= seg_alloc();
177 ldef("..gotoff", elf_gotoff_sect
+1, 0L, NULL
, FALSE
, FALSE
,&of_elf
,error
);
178 elf_got_sect
= seg_alloc();
179 ldef("..got", elf_got_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
180 elf_plt_sect
= seg_alloc();
181 ldef("..plt", elf_plt_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
182 elf_sym_sect
= seg_alloc();
183 ldef("..sym", elf_sym_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
185 def_seg
= seg_alloc();
188 static void elf_cleanup(int debuginfo
)
197 for (i
=0; i
<nsects
; i
++) {
198 if (sects
[i
]->type
!= SHT_NOBITS
)
199 saa_free (sects
[i
]->data
);
201 saa_free (sects
[i
]->rel
);
202 while (sects
[i
]->head
) {
204 sects
[i
]->head
= sects
[i
]->head
->next
;
214 static void add_sectname (char *firsthalf
, char *secondhalf
)
216 int len
= strlen(firsthalf
)+strlen(secondhalf
);
217 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
218 shstrtab
= nasm_realloc (shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
219 strcpy (shstrtab
+shstrtablen
, firsthalf
);
220 strcat (shstrtab
+shstrtablen
, secondhalf
);
221 shstrtablen
+= len
+1;
224 static int elf_make_section (char *name
, int type
, int flags
, int align
)
228 s
= nasm_malloc (sizeof(*s
));
230 if (type
!= SHT_NOBITS
)
231 s
->data
= saa_init (1L);
234 s
->len
= s
->size
= 0;
236 if (!strcmp(name
, ".text"))
239 s
->index
= seg_alloc();
240 add_sectname ("", name
);
241 s
->name
= nasm_malloc (1+strlen(name
));
242 strcpy (s
->name
, name
);
248 if (nsects
>= sectlen
)
249 sects
= nasm_realloc (sects
, (sectlen
+= SECT_DELTA
)*sizeof(*sects
));
255 static long elf_section_names (char *name
, int pass
, int *bits
)
258 int flags_and
, flags_or
, type
, align
, i
;
261 * Default is 32 bits.
269 while (*p
&& !isspace(*p
)) p
++;
271 flags_and
= flags_or
= type
= align
= 0;
273 while (*p
&& isspace(*p
)) p
++;
276 while (*p
&& !isspace(*p
)) p
++;
278 while (*p
&& isspace(*p
)) p
++;
280 if (!nasm_strnicmp(q
, "align=", 6)) {
284 if ( (align
-1) & align
) { /* means it's not a power of two */
285 error (ERR_NONFATAL
, "section alignment %d is not"
286 " a power of two", align
);
289 } else if (!nasm_stricmp(q
, "alloc")) {
290 flags_and
|= SHF_ALLOC
;
291 flags_or
|= SHF_ALLOC
;
292 } else if (!nasm_stricmp(q
, "noalloc")) {
293 flags_and
|= SHF_ALLOC
;
294 flags_or
&= ~SHF_ALLOC
;
295 } else if (!nasm_stricmp(q
, "exec")) {
296 flags_and
|= SHF_EXECINSTR
;
297 flags_or
|= SHF_EXECINSTR
;
298 } else if (!nasm_stricmp(q
, "noexec")) {
299 flags_and
|= SHF_EXECINSTR
;
300 flags_or
&= ~SHF_EXECINSTR
;
301 } else if (!nasm_stricmp(q
, "write")) {
302 flags_and
|= SHF_WRITE
;
303 flags_or
|= SHF_WRITE
;
304 } else if (!nasm_stricmp(q
, "nowrite")) {
305 flags_and
|= SHF_WRITE
;
306 flags_or
&= ~SHF_WRITE
;
307 } else if (!nasm_stricmp(q
, "progbits")) {
309 } else if (!nasm_stricmp(q
, "nobits")) {
314 if (!strcmp(name
, ".comment") ||
315 !strcmp(name
, ".shstrtab") ||
316 !strcmp(name
, ".symtab") ||
317 !strcmp(name
, ".strtab")) {
318 error (ERR_NONFATAL
, "attempt to redefine reserved section"
323 for (i
=0; i
<nsects
; i
++)
324 if (!strcmp(name
, sects
[i
]->name
))
327 if (!strcmp(name
, ".text"))
328 i
= elf_make_section (name
, SHT_PROGBITS
,
329 SHF_ALLOC
| SHF_EXECINSTR
, 16);
330 else if (!strcmp(name
, ".data"))
331 i
= elf_make_section (name
, SHT_PROGBITS
,
332 SHF_ALLOC
| SHF_WRITE
, 4);
333 else if (!strcmp(name
, ".bss"))
334 i
= elf_make_section (name
, SHT_NOBITS
,
335 SHF_ALLOC
| SHF_WRITE
, 4);
337 i
= elf_make_section (name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
339 sects
[i
]->type
= type
;
341 sects
[i
]->align
= align
;
342 sects
[i
]->flags
&= ~flags_and
;
343 sects
[i
]->flags
|= flags_or
;
344 } else if (pass
== 1) {
345 if (type
|| align
|| flags_and
)
346 error (ERR_WARNING
, "section attributes ignored on"
347 " redeclaration of section `%s'", name
);
350 return sects
[i
]->index
;
353 static void elf_deflabel (char *name
, long segment
, long offset
,
354 int is_global
, char *special
)
358 int special_used
= FALSE
;
360 #if defined(DEBUG) && DEBUG>2
361 fprintf(stderr
, " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
362 name
, segment
, offset
, is_global
, special
);
364 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
366 * This is a NASM special symbol. We never allow it into
367 * the ELF symbol table, even if it's a valid one. If it
368 * _isn't_ a valid one, we should barf immediately.
370 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
371 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
372 strcmp(name
, "..sym"))
373 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
377 if (is_global
== 3) {
380 * Fix up a forward-reference symbol size from the first
383 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
384 if (!strcmp((*s
)->name
, name
)) {
385 struct tokenval tokval
;
389 while (*p
&& !isspace(*p
)) p
++;
390 while (*p
&& isspace(*p
)) p
++;
393 tokval
.t_type
= TOKEN_INVALID
;
394 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
397 error (ERR_NONFATAL
, "cannot use relocatable"
398 " expression as symbol size");
400 (*s
)->size
= reloc_value(e
);
404 * Remove it from the list of unresolved sizes.
406 nasm_free ((*s
)->name
);
410 return; /* it wasn't an important one */
413 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
414 strslen
+= 1+strlen(name
);
416 sym
= saa_wstruct (syms
);
419 sym
->type
= is_global
? SYM_GLOBAL
: 0;
421 if (segment
== NO_SEG
)
422 sym
->section
= SHN_ABS
;
425 sym
->section
= SHN_UNDEF
;
426 if (nsects
== 0 && segment
== def_seg
) {
428 if (segment
!= elf_section_names (".text", 2, &tempint
))
429 error (ERR_PANIC
, "strange segment conditions in ELF driver");
430 sym
->section
= nsects
;
432 for (i
=0; i
<nsects
; i
++)
433 if (segment
== sects
[i
]->index
) {
440 if (is_global
== 2) {
443 sym
->section
= SHN_COMMON
;
445 * We have a common variable. Check the special text to see
446 * if it's a valid number and power of two; if so, store it
447 * as the alignment for the common variable.
451 sym
->value
= readnum (special
, &err
);
453 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
454 " valid number", special
);
455 else if ( (sym
->value
| (sym
->value
-1)) != 2*sym
->value
- 1)
456 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
457 " power of two", special
);
461 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
463 if (sym
->type
== SYM_GLOBAL
) {
465 * There's a problem here that needs fixing.
466 * If sym->section == SHN_ABS, then the first line of the
467 * else section causes a core dump, because its a reference
468 * beyond the end of the section array.
469 * This behaviour is exhibited by this code:
473 * I'm not sure how to procede, because I haven't got the
474 * first clue about how ELF works, so I don't know what to
475 * do with it. Furthermore, I'm not sure what the rest of this
476 * section of code does. Help?
478 * For now, I'll see if doing absolutely nothing with it will
481 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
483 bsym
= raa_write (bsym
, segment
, nglobs
);
485 else if (sym
->section
!= SHN_ABS
)
488 * This is a global symbol; so we must add it to the linked
489 * list of global symbols in its section. We'll push it on
490 * the beginning of the list, because it doesn't matter
491 * much which end we put it on and it's easier like this.
493 * In addition, we check the special text for symbol
494 * type and size information.
496 sym
->next
= sects
[sym
->section
-1]->gsyms
;
497 sects
[sym
->section
-1]->gsyms
= sym
;
500 int n
= strcspn(special
, " ");
502 if (!nasm_strnicmp(special
, "function", n
))
503 sym
->type
|= SYM_FUNCTION
;
504 else if (!nasm_strnicmp(special
, "data", n
) ||
505 !nasm_strnicmp(special
, "object", n
))
506 sym
->type
|= SYM_DATA
;
508 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
511 struct tokenval tokval
;
514 char *saveme
=stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
516 while (special
[n
] && isspace(special
[n
]))
519 * We have a size expression; attempt to
523 stdscan_bufptr
= special
+n
;
524 tokval
.t_type
= TOKEN_INVALID
;
525 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
, NULL
);
529 sym
->name
= nasm_strdup(name
);
532 error (ERR_NONFATAL
, "cannot use relocatable"
533 " expression as symbol size");
535 sym
->size
= reloc_value(e
);
537 stdscan_bufptr
=saveme
; /* bugfix? fbk 8/10/00 */
542 sym
->globnum
= nglobs
;
547 if (special
&& !special_used
)
548 error(ERR_NONFATAL
, "no special symbol features supported here");
551 static void elf_add_reloc (struct Section
*sect
, long segment
,
556 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
557 sect
->tail
= &r
->next
;
560 r
->address
= sect
->len
;
561 if (segment
== NO_SEG
)
566 for (i
=0; i
<nsects
; i
++)
567 if (segment
== sects
[i
]->index
)
570 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
578 * This routine deals with ..got and ..sym relocations: the more
579 * complicated kinds. In shared-library writing, some relocations
580 * with respect to global symbols must refer to the precise symbol
581 * rather than referring to an offset from the base of the section
582 * _containing_ the symbol. Such relocations call to this routine,
583 * which searches the symbol list for the symbol in question.
585 * R_386_GOT32 references require the _exact_ symbol address to be
586 * used; R_386_32 references can be at an offset from the symbol.
587 * The boolean argument `exact' tells us this.
589 * Return value is the adjusted value of `addr', having become an
590 * offset from the symbol rather than the section. Should always be
591 * zero when returning from an exact call.
593 * Limitation: if you define two symbols at the same place,
594 * confusion will occur.
596 * Inefficiency: we search, currently, using a linked list which
597 * isn't even necessarily sorted.
599 static long elf_add_gsym_reloc (struct Section
*sect
,
600 long segment
, long offset
,
605 struct Symbol
*sym
, *sm
;
609 * First look up the segment/offset pair and find a global
610 * symbol corresponding to it. If it's not one of our segments,
611 * then it must be an external symbol, in which case we're fine
612 * doing a normal elf_add_reloc after first sanity-checking
613 * that the offset from the symbol is zero.
616 for (i
=0; i
<nsects
; i
++)
617 if (segment
== sects
[i
]->index
) {
622 if (exact
&& offset
!= 0)
623 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
624 " for this reference");
626 elf_add_reloc (sect
, segment
, type
);
632 * Find a symbol pointing _exactly_ at this one.
634 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
635 if (sym
->value
== offset
)
639 * Find the nearest symbol below this one.
642 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
643 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
647 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
648 " for this reference");
652 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
653 sect
->tail
= &r
->next
;
656 r
->address
= sect
->len
;
657 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
662 return offset
- sym
->value
;
665 static void elf_out (long segto
, void *data
, unsigned long type
,
666 long segment
, long wrt
)
669 long realbytes
= type
& OUT_SIZMASK
;
671 unsigned char mydata
[4], *p
;
677 * handle absolute-assembly (structure definitions)
679 if (segto
== NO_SEG
) {
680 if (type
!= OUT_RESERVE
)
681 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
687 for (i
=0; i
<nsects
; i
++)
688 if (segto
== sects
[i
]->index
) {
693 int tempint
; /* ignored */
694 if (segto
!= elf_section_names (".text", 2, &tempint
))
695 error (ERR_PANIC
, "strange segment conditions in ELF driver");
700 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
701 error(ERR_WARNING
, "attempt to initialise memory in"
702 " BSS section `%s': ignored", s
->name
);
703 if (type
== OUT_REL2ADR
)
705 else if (type
== OUT_REL4ADR
)
711 if (type
== OUT_RESERVE
) {
712 if (s
->type
== SHT_PROGBITS
) {
713 error(ERR_WARNING
, "uninitialised space declared in"
714 " non-BSS section `%s': zeroing", s
->name
);
715 elf_sect_write (s
, NULL
, realbytes
);
718 } else if (type
== OUT_RAWDATA
) {
719 if (segment
!= NO_SEG
)
720 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
721 elf_sect_write (s
, data
, realbytes
);
722 } else if (type
== OUT_ADDRESS
) {
724 addr
= *(long *)data
;
725 if (segment
!= NO_SEG
) {
727 error(ERR_NONFATAL
, "ELF format does not support"
728 " segment base references");
731 if ( realbytes
== 2 ) {
733 elf_add_reloc (s
, segment
, R_386_16
);
735 elf_add_reloc (s
, segment
, R_386_32
);
737 } else if (wrt
== elf_gotpc_sect
+1) {
739 * The user will supply GOT relative to $$. ELF
740 * will let us have GOT relative to $. So we
741 * need to fix up the data item by $-$$.
744 elf_add_reloc (s
, segment
, R_386_GOTPC
);
745 } else if (wrt
== elf_gotoff_sect
+1) {
746 elf_add_reloc (s
, segment
, R_386_GOTOFF
);
747 } else if (wrt
== elf_got_sect
+1) {
748 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
750 } else if (wrt
== elf_sym_sect
+1) {
751 if ( realbytes
== 2 ) {
753 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
756 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
759 } else if (wrt
== elf_plt_sect
+1) {
760 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
761 "relative PLT references");
763 error (ERR_NONFATAL
, "ELF format does not support this"
765 wrt
= NO_SEG
; /* we can at least _try_ to continue */
771 error(ERR_WARNING
|ERR_WARN_GNUELF
,
772 "16-bit relocations in ELF is a GNU extension");
773 WRITESHORT (p
, addr
);
775 if (realbytes
!= 4 && segment
!= NO_SEG
) {
776 error (ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
780 elf_sect_write (s
, mydata
, realbytes
);
781 } else if (type
== OUT_REL2ADR
) {
782 if (segment
== segto
)
783 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
784 if (segment
!= NO_SEG
&& segment
% 2) {
785 error(ERR_NONFATAL
, "ELF format does not support"
786 " segment base references");
789 error (ERR_WARNING
|ERR_WARN_GNUELF
,
790 "16-bit relocations in ELF is a GNU extension");
791 elf_add_reloc (s
, segment
, R_386_PC16
);
793 error (ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
797 WRITESHORT (p
, *(long*)data
- realbytes
);
798 elf_sect_write (s
, mydata
, 2L);
799 } else if (type
== OUT_REL4ADR
) {
800 if (segment
== segto
)
801 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
802 if (segment
!= NO_SEG
&& segment
% 2) {
803 error(ERR_NONFATAL
, "ELF format does not support"
804 " segment base references");
807 elf_add_reloc (s
, segment
, R_386_PC32
);
808 } else if (wrt
== elf_plt_sect
+1) {
809 elf_add_reloc (s
, segment
, R_386_PLT32
);
810 } else if (wrt
== elf_gotpc_sect
+1 ||
811 wrt
== elf_gotoff_sect
+1 ||
812 wrt
== elf_got_sect
+1) {
813 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
814 "relative GOT references");
816 error (ERR_NONFATAL
, "ELF format does not support this"
818 wrt
= NO_SEG
; /* we can at least _try_ to continue */
822 WRITELONG (p
, *(long*)data
- realbytes
);
823 elf_sect_write (s
, mydata
, 4L);
827 static void elf_write(void)
829 int nsections
, align
;
836 long symtablen
, symtablocal
;
839 * Work out how many sections we will have. We have SHN_UNDEF,
840 * then the flexible user sections, then the four fixed
841 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
842 * then optionally relocation sections for the user sections.
844 nsections
= 5; /* SHN_UNDEF and the fixed ones */
845 add_sectname ("", ".comment");
846 add_sectname ("", ".shstrtab");
847 add_sectname ("", ".symtab");
848 add_sectname ("", ".strtab");
849 for (i
=0; i
<nsects
; i
++) {
850 nsections
++; /* for the section itself */
851 if (sects
[i
]->head
) {
852 nsections
++; /* for its relocations */
853 add_sectname (".rel", sects
[i
]->name
);
861 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
864 * Output the ELF header.
866 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
867 fwriteshort (1, elffp
); /* ET_REL relocatable file */
868 fwriteshort (3, elffp
); /* EM_386 processor ID */
869 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
870 fwritelong (0L, elffp
); /* no entry point */
871 fwritelong (0L, elffp
); /* no program header table */
872 fwritelong (0x40L
, elffp
); /* section headers straight after
873 * ELF header plus alignment */
874 fwritelong (0L, elffp
); /* 386 defines no special flags */
875 fwriteshort (0x34, elffp
); /* size of ELF header */
876 fwriteshort (0, elffp
); /* no program header table, again */
877 fwriteshort (0, elffp
); /* still no program header table */
878 fwriteshort (0x28, elffp
); /* size of section header */
879 fwriteshort (nsections
, elffp
); /* number of sections */
880 fwriteshort (nsects
+2, elffp
); /* string table section index for
881 * section header table */
882 fwritelong (0L, elffp
); /* align to 0x40 bytes */
883 fwritelong (0L, elffp
);
884 fwritelong (0L, elffp
);
887 * Build the symbol table and relocation tables.
889 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
890 for (i
=0; i
<nsects
; i
++)
892 sects
[i
]->rel
= elf_build_reltab (§s
[i
]->rellen
,
896 * Now output the section header table.
899 elf_foffs
= 0x40 + 0x28 * nsections
;
900 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
903 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
905 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
907 for (i
=0; i
<nsects
; i
++) {
908 elf_section_header (p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
909 (sects
[i
]->type
== SHT_PROGBITS
?
910 sects
[i
]->data
: NULL
), TRUE
,
911 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
914 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
915 (long)commlen
, 0, 0, 1, 0);/* .comment */
917 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
918 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
920 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
921 symtablen
, nsects
+4, symtablocal
, 4, 16);/* .symtab */
923 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
924 strslen
, 0, 0, 1, 0); /* .strtab */
925 for (i
=0; i
<nsects
; i
++) if (sects
[i
]->head
) {
927 elf_section_header (p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
928 sects
[i
]->rellen
, nsects
+3, i
+1, 4, 8);
931 fwrite (align_str
, align
, 1, elffp
);
934 * Now output the sections.
936 elf_write_sections();
938 nasm_free (elf_sects
);
942 static struct SAA
*elf_build_symtab (long *len
, long *local
)
944 struct SAA
*s
= saa_init(1L);
946 unsigned char entry
[16], *p
;
952 * First, an all-zeros entry, required by the ELF spec.
954 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
959 * Next, an entry for the file name.
962 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
963 WRITELONG (p
, 0); /* no value */
964 WRITELONG (p
, 0); /* no size either */
965 WRITESHORT (p
, 4); /* type FILE */
966 WRITESHORT (p
, SHN_ABS
);
967 saa_wbytes (s
, entry
, 16L);
972 * Now some standard symbols defining the segments, for relocation
975 for (i
= 1; i
<= nsects
+1; i
++) {
977 WRITELONG (p
, 0); /* no symbol name */
978 WRITELONG (p
, 0); /* offset zero */
979 WRITELONG (p
, 0); /* size zero */
980 WRITESHORT (p
, 3); /* local section-type thing */
981 WRITESHORT (p
, (i
==1 ? SHN_ABS
: i
-1)); /* the section id */
982 saa_wbytes (s
, entry
, 16L);
988 * Now the other local symbols.
991 while ( (sym
= saa_rstruct (syms
)) ) {
992 if (sym
->type
& SYM_GLOBAL
)
995 WRITELONG (p
, sym
->strpos
);
996 WRITELONG (p
, sym
->value
);
997 WRITELONG (p
, sym
->size
);
998 WRITESHORT (p
, sym
->type
); /* local non-typed thing */
999 WRITESHORT (p
, sym
->section
);
1000 saa_wbytes (s
, entry
, 16L);
1006 * Now the global symbols.
1009 while ( (sym
= saa_rstruct (syms
)) ) {
1010 if (!(sym
->type
& SYM_GLOBAL
))
1013 WRITELONG (p
, sym
->strpos
);
1014 WRITELONG (p
, sym
->value
);
1015 WRITELONG (p
, sym
->size
);
1016 WRITESHORT (p
, sym
->type
); /* global non-typed thing */
1017 WRITESHORT (p
, sym
->section
);
1018 saa_wbytes (s
, entry
, 16L);
1025 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
1027 unsigned char *p
, entry
[8];
1036 long sym
= r
->symbol
;
1038 if (sym
>= GLOBAL_TEMP_BASE
)
1039 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+3) + nlocals
;
1042 WRITELONG (p
, r
->address
);
1043 WRITELONG (p
, (sym
<< 8) + r
->type
);
1044 saa_wbytes (s
, entry
, 8L);
1053 static void elf_section_header (int name
, int type
, int flags
,
1054 void *data
, int is_saa
, long datalen
,
1055 int link
, int info
, int align
, int eltsize
)
1057 elf_sects
[elf_nsect
].data
= data
;
1058 elf_sects
[elf_nsect
].len
= datalen
;
1059 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1062 fwritelong ((long)name
, elffp
);
1063 fwritelong ((long)type
, elffp
);
1064 fwritelong ((long)flags
, elffp
);
1065 fwritelong (0L, elffp
); /* no address, ever, in object files */
1066 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
1067 fwritelong (datalen
, elffp
);
1069 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1070 fwritelong ((long)link
, elffp
);
1071 fwritelong ((long)info
, elffp
);
1072 fwritelong ((long)align
, elffp
);
1073 fwritelong ((long)eltsize
, elffp
);
1076 static void elf_write_sections (void)
1079 for (i
= 0; i
< elf_nsect
; i
++)
1080 if (elf_sects
[i
].data
) {
1081 long len
= elf_sects
[i
].len
;
1082 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1083 long align
= reallen
- len
;
1084 if (elf_sects
[i
].is_saa
)
1085 saa_fpwrite (elf_sects
[i
].data
, elffp
);
1087 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
1088 fwrite (align_str
, align
, 1, elffp
);
1092 static void elf_sect_write (struct Section
*sect
,
1093 unsigned char *data
, unsigned long len
)
1095 saa_wbytes (sect
->data
, data
, len
);
1099 static long elf_segbase (long segment
)
1104 static int elf_directive (char *directive
, char *value
, int pass
)
1109 static void elf_filename (char *inname
, char *outname
, efunc error
)
1111 strcpy(elf_module
, inname
);
1112 standard_extension (inname
, outname
, ".o", error
);
1115 static char *elf_stdmac
[] = {
1116 "%define __SECT__ [section .text]",
1117 "%macro __NASM_CDecl__ 1",
1122 static int elf_set_info(enum geninfo type
, char **val
)
1127 struct ofmt of_elf
= {
1128 "ELF32 (i386) object files (e.g. Linux)",