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 16 /* 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
*, const 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 /* this stuff is needed for the stabs debugging format */
146 #define N_SO 0x64 /* ID for main source file */
147 #define N_SOL 0x84 /* ID for sub-source file */
151 #define TY_STABSSYMLIN 0x40 /* ouch */
154 unsigned long n_strx
;
155 unsigned char n_type
;
156 unsigned char n_other
;
157 unsigned short n_desc
;
158 unsigned long n_value
;
167 int section
; /* section index */
168 char *name
; /* shallow-copied pointer of section name */
172 struct symlininfo info
;
175 struct linelist
* next
;
176 struct linelist
* last
;
179 static struct linelist
*stabslines
=0;
180 static int stabs_immcall
=0;
181 static int currentline
=0;
182 static int numlinestabs
=0;
183 static char * stabs_filename
=0;
184 static int symtabsection
;
185 static unsigned char *stabbuf
=0,*stabstrbuf
=0,*stabrelbuf
=0;
186 static int stablen
,stabstrlen
,stabrellen
;
188 static struct dfmt df_stabs
;
190 void stabs_init(struct ofmt
*,void *,FILE *,efunc
);
191 void stabs_linenum(const char *filename
,long linenumber
,long);
192 void stabs_deflabel(char *,long ,long ,int ,char *);
193 void stabs_directive(const char *,const char *);
194 void stabs_typevalue(long );
195 void stabs_output(int ,void *);
196 void stabs_generate();
197 void stabs_cleanup();
199 /* end of stabs debugging stuff */
203 * Special section numbers which are used to define ELF special
204 * symbols, which can be used with WRT to provide PIC relocation
207 static long elf_gotpc_sect
, elf_gotoff_sect
;
208 static long elf_got_sect
, elf_plt_sect
;
209 static long elf_sym_sect
;
211 static void elf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
216 (void) ldef
; /* placate optimisers */
218 nsects
= sectlen
= 0;
219 syms
= saa_init((long)sizeof(struct Symbol
));
220 nlocals
= nglobs
= 0;
223 saa_wbytes (strs
, "\0", 1L);
224 saa_wbytes (strs
, elf_module
, (long)(strlen(elf_module
)+1));
225 strslen
= 2+strlen(elf_module
);
227 shstrtablen
= shstrtabsize
= 0;;
228 add_sectname ("", "");
232 elf_gotpc_sect
= seg_alloc();
233 ldef("..gotpc", elf_gotpc_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
234 elf_gotoff_sect
= seg_alloc();
235 ldef("..gotoff", elf_gotoff_sect
+1, 0L, NULL
, FALSE
, FALSE
,&of_elf
,error
);
236 elf_got_sect
= seg_alloc();
237 ldef("..got", elf_got_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
238 elf_plt_sect
= seg_alloc();
239 ldef("..plt", elf_plt_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
240 elf_sym_sect
= seg_alloc();
241 ldef("..sym", elf_sym_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
243 def_seg
= seg_alloc();
246 static void elf_cleanup(int debuginfo
)
255 for (i
=0; i
<nsects
; i
++) {
256 if (sects
[i
]->type
!= SHT_NOBITS
)
257 saa_free (sects
[i
]->data
);
259 saa_free (sects
[i
]->rel
);
260 while (sects
[i
]->head
) {
262 sects
[i
]->head
= sects
[i
]->head
->next
;
270 if (of_elf
.current_dfmt
) {
271 of_elf
.current_dfmt
->cleanup();
275 static void add_sectname (char *firsthalf
, char *secondhalf
)
277 int len
= strlen(firsthalf
)+strlen(secondhalf
);
278 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
279 shstrtab
= nasm_realloc (shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
280 strcpy (shstrtab
+shstrtablen
, firsthalf
);
281 strcat (shstrtab
+shstrtablen
, secondhalf
);
282 shstrtablen
+= len
+1;
285 static int elf_make_section (char *name
, int type
, int flags
, int align
)
289 s
= nasm_malloc (sizeof(*s
));
291 if (type
!= SHT_NOBITS
)
292 s
->data
= saa_init (1L);
295 s
->len
= s
->size
= 0;
297 if (!strcmp(name
, ".text"))
300 s
->index
= seg_alloc();
301 add_sectname ("", name
);
302 s
->name
= nasm_malloc (1+strlen(name
));
303 strcpy (s
->name
, name
);
309 if (nsects
>= sectlen
)
310 sects
= nasm_realloc (sects
, (sectlen
+= SECT_DELTA
)*sizeof(*sects
));
316 static long elf_section_names (char *name
, int pass
, int *bits
)
319 int flags_and
, flags_or
, type
, align
, i
;
322 * Default is 32 bits.
330 while (*p
&& !isspace(*p
)) p
++;
332 flags_and
= flags_or
= type
= align
= 0;
334 while (*p
&& isspace(*p
)) p
++;
337 while (*p
&& !isspace(*p
)) p
++;
339 while (*p
&& isspace(*p
)) p
++;
341 if (!nasm_strnicmp(q
, "align=", 6)) {
345 if ( (align
-1) & align
) { /* means it's not a power of two */
346 error (ERR_NONFATAL
, "section alignment %d is not"
347 " a power of two", align
);
350 } else if (!nasm_stricmp(q
, "alloc")) {
351 flags_and
|= SHF_ALLOC
;
352 flags_or
|= SHF_ALLOC
;
353 } else if (!nasm_stricmp(q
, "noalloc")) {
354 flags_and
|= SHF_ALLOC
;
355 flags_or
&= ~SHF_ALLOC
;
356 } else if (!nasm_stricmp(q
, "exec")) {
357 flags_and
|= SHF_EXECINSTR
;
358 flags_or
|= SHF_EXECINSTR
;
359 } else if (!nasm_stricmp(q
, "noexec")) {
360 flags_and
|= SHF_EXECINSTR
;
361 flags_or
&= ~SHF_EXECINSTR
;
362 } else if (!nasm_stricmp(q
, "write")) {
363 flags_and
|= SHF_WRITE
;
364 flags_or
|= SHF_WRITE
;
365 } else if (!nasm_stricmp(q
, "nowrite")) {
366 flags_and
|= SHF_WRITE
;
367 flags_or
&= ~SHF_WRITE
;
368 } else if (!nasm_stricmp(q
, "progbits")) {
370 } else if (!nasm_stricmp(q
, "nobits")) {
375 if (!strcmp(name
, ".comment") ||
376 !strcmp(name
, ".shstrtab") ||
377 !strcmp(name
, ".symtab") ||
378 !strcmp(name
, ".strtab")) {
379 error (ERR_NONFATAL
, "attempt to redefine reserved section"
384 for (i
=0; i
<nsects
; i
++)
385 if (!strcmp(name
, sects
[i
]->name
))
388 if (!strcmp(name
, ".text"))
389 i
= elf_make_section (name
, SHT_PROGBITS
,
390 SHF_ALLOC
| SHF_EXECINSTR
, 16);
391 else if (!strcmp(name
, ".rodata"))
392 i
= elf_make_section (name
, SHT_PROGBITS
,
394 else if (!strcmp(name
, ".data"))
395 i
= elf_make_section (name
, SHT_PROGBITS
,
396 SHF_ALLOC
| SHF_WRITE
, 4);
397 else if (!strcmp(name
, ".bss"))
398 i
= elf_make_section (name
, SHT_NOBITS
,
399 SHF_ALLOC
| SHF_WRITE
, 4);
401 i
= elf_make_section (name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
403 sects
[i
]->type
= type
;
405 sects
[i
]->align
= align
;
406 sects
[i
]->flags
&= ~flags_and
;
407 sects
[i
]->flags
|= flags_or
;
408 } else if (pass
== 1) {
409 if (type
|| align
|| flags_and
)
410 error (ERR_WARNING
, "section attributes ignored on"
411 " redeclaration of section `%s'", name
);
414 return sects
[i
]->index
;
417 static void elf_deflabel (char *name
, long segment
, long offset
,
418 int is_global
, char *special
)
422 int special_used
= FALSE
;
424 #if defined(DEBUG) && DEBUG>2
425 fprintf(stderr
, " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
426 name
, segment
, offset
, is_global
, special
);
428 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
430 * This is a NASM special symbol. We never allow it into
431 * the ELF symbol table, even if it's a valid one. If it
432 * _isn't_ a valid one, we should barf immediately.
434 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
435 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
436 strcmp(name
, "..sym"))
437 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
441 if (is_global
== 3) {
444 * Fix up a forward-reference symbol size from the first
447 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
448 if (!strcmp((*s
)->name
, name
)) {
449 struct tokenval tokval
;
453 while (*p
&& !isspace(*p
)) p
++;
454 while (*p
&& isspace(*p
)) p
++;
457 tokval
.t_type
= TOKEN_INVALID
;
458 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
461 error (ERR_NONFATAL
, "cannot use relocatable"
462 " expression as symbol size");
464 (*s
)->size
= reloc_value(e
);
468 * Remove it from the list of unresolved sizes.
470 nasm_free ((*s
)->name
);
474 return; /* it wasn't an important one */
477 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
478 strslen
+= 1+strlen(name
);
480 sym
= saa_wstruct (syms
);
483 sym
->type
= is_global
? SYM_GLOBAL
: 0;
485 if (segment
== NO_SEG
)
486 sym
->section
= SHN_ABS
;
489 sym
->section
= SHN_UNDEF
;
490 if (nsects
== 0 && segment
== def_seg
) {
492 if (segment
!= elf_section_names (".text", 2, &tempint
))
493 error (ERR_PANIC
, "strange segment conditions in ELF driver");
494 sym
->section
= nsects
;
496 for (i
=0; i
<nsects
; i
++)
497 if (segment
== sects
[i
]->index
) {
504 if (is_global
== 2) {
507 sym
->section
= SHN_COMMON
;
509 * We have a common variable. Check the special text to see
510 * if it's a valid number and power of two; if so, store it
511 * as the alignment for the common variable.
515 sym
->value
= readnum (special
, &err
);
517 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
518 " valid number", special
);
519 else if ( (sym
->value
| (sym
->value
-1)) != 2*sym
->value
- 1)
520 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
521 " power of two", special
);
525 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
527 if (sym
->type
== SYM_GLOBAL
) {
529 * There's a problem here that needs fixing.
530 * If sym->section == SHN_ABS, then the first line of the
531 * else section causes a core dump, because its a reference
532 * beyond the end of the section array.
533 * This behaviour is exhibited by this code:
537 * I'm not sure how to procede, because I haven't got the
538 * first clue about how ELF works, so I don't know what to
539 * do with it. Furthermore, I'm not sure what the rest of this
540 * section of code does. Help?
542 * For now, I'll see if doing absolutely nothing with it will
545 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
547 bsym
= raa_write (bsym
, segment
, nglobs
);
549 else if (sym
->section
!= SHN_ABS
)
552 * This is a global symbol; so we must add it to the linked
553 * list of global symbols in its section. We'll push it on
554 * the beginning of the list, because it doesn't matter
555 * much which end we put it on and it's easier like this.
557 * In addition, we check the special text for symbol
558 * type and size information.
560 sym
->next
= sects
[sym
->section
-1]->gsyms
;
561 sects
[sym
->section
-1]->gsyms
= sym
;
564 int n
= strcspn(special
, " ");
566 if (!nasm_strnicmp(special
, "function", n
))
567 sym
->type
|= SYM_FUNCTION
;
568 else if (!nasm_strnicmp(special
, "data", n
) ||
569 !nasm_strnicmp(special
, "object", n
))
570 sym
->type
|= SYM_DATA
;
572 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
575 struct tokenval tokval
;
578 char *saveme
=stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
580 while (special
[n
] && isspace(special
[n
]))
583 * We have a size expression; attempt to
587 stdscan_bufptr
= special
+n
;
588 tokval
.t_type
= TOKEN_INVALID
;
589 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
, NULL
);
593 sym
->name
= nasm_strdup(name
);
596 error (ERR_NONFATAL
, "cannot use relocatable"
597 " expression as symbol size");
599 sym
->size
= reloc_value(e
);
601 stdscan_bufptr
=saveme
; /* bugfix? fbk 8/10/00 */
606 sym
->globnum
= nglobs
;
611 if (special
&& !special_used
)
612 error(ERR_NONFATAL
, "no special symbol features supported here");
615 static void elf_add_reloc (struct Section
*sect
, long segment
,
620 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
621 sect
->tail
= &r
->next
;
624 r
->address
= sect
->len
;
625 if (segment
== NO_SEG
)
630 for (i
=0; i
<nsects
; i
++)
631 if (segment
== sects
[i
]->index
)
634 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
642 * This routine deals with ..got and ..sym relocations: the more
643 * complicated kinds. In shared-library writing, some relocations
644 * with respect to global symbols must refer to the precise symbol
645 * rather than referring to an offset from the base of the section
646 * _containing_ the symbol. Such relocations call to this routine,
647 * which searches the symbol list for the symbol in question.
649 * R_386_GOT32 references require the _exact_ symbol address to be
650 * used; R_386_32 references can be at an offset from the symbol.
651 * The boolean argument `exact' tells us this.
653 * Return value is the adjusted value of `addr', having become an
654 * offset from the symbol rather than the section. Should always be
655 * zero when returning from an exact call.
657 * Limitation: if you define two symbols at the same place,
658 * confusion will occur.
660 * Inefficiency: we search, currently, using a linked list which
661 * isn't even necessarily sorted.
663 static long elf_add_gsym_reloc (struct Section
*sect
,
664 long segment
, long offset
,
669 struct Symbol
*sym
, *sm
;
673 * First look up the segment/offset pair and find a global
674 * symbol corresponding to it. If it's not one of our segments,
675 * then it must be an external symbol, in which case we're fine
676 * doing a normal elf_add_reloc after first sanity-checking
677 * that the offset from the symbol is zero.
680 for (i
=0; i
<nsects
; i
++)
681 if (segment
== sects
[i
]->index
) {
686 if (exact
&& offset
!= 0)
687 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
688 " for this reference");
690 elf_add_reloc (sect
, segment
, type
);
696 * Find a symbol pointing _exactly_ at this one.
698 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
699 if (sym
->value
== offset
)
703 * Find the nearest symbol below this one.
706 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
707 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
711 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
712 " for this reference");
716 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
717 sect
->tail
= &r
->next
;
720 r
->address
= sect
->len
;
721 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
726 return offset
- sym
->value
;
729 static void elf_out (long segto
, const void *data
, unsigned long type
,
730 long segment
, long wrt
)
733 long realbytes
= type
& OUT_SIZMASK
;
735 unsigned char mydata
[4], *p
;
737 static struct symlininfo sinfo
;
742 * handle absolute-assembly (structure definitions)
744 if (segto
== NO_SEG
) {
745 if (type
!= OUT_RESERVE
)
746 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
752 for (i
=0; i
<nsects
; i
++)
753 if (segto
== sects
[i
]->index
) {
758 int tempint
; /* ignored */
759 if (segto
!= elf_section_names (".text", 2, &tempint
))
760 error (ERR_PANIC
, "strange segment conditions in ELF driver");
767 /* again some stabs debugging stuff */
768 if (of_elf
.current_dfmt
) {
772 of_elf
.current_dfmt
->debug_output(TY_STABSSYMLIN
,&sinfo
);
774 /* end of debugging stuff */
776 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
777 error(ERR_WARNING
, "attempt to initialise memory in"
778 " BSS section `%s': ignored", s
->name
);
779 if (type
== OUT_REL2ADR
)
781 else if (type
== OUT_REL4ADR
)
787 if (type
== OUT_RESERVE
) {
788 if (s
->type
== SHT_PROGBITS
) {
789 error(ERR_WARNING
, "uninitialised space declared in"
790 " non-BSS section `%s': zeroing", s
->name
);
791 elf_sect_write (s
, NULL
, realbytes
);
794 } else if (type
== OUT_RAWDATA
) {
795 if (segment
!= NO_SEG
)
796 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
797 elf_sect_write (s
, data
, realbytes
);
798 } else if (type
== OUT_ADDRESS
) {
800 addr
= *(long *)data
;
801 if (segment
!= NO_SEG
) {
803 error(ERR_NONFATAL
, "ELF format does not support"
804 " segment base references");
807 if ( realbytes
== 2 ) {
809 elf_add_reloc (s
, segment
, R_386_16
);
811 elf_add_reloc (s
, segment
, R_386_32
);
813 } else if (wrt
== elf_gotpc_sect
+1) {
815 * The user will supply GOT relative to $$. ELF
816 * will let us have GOT relative to $. So we
817 * need to fix up the data item by $-$$.
820 elf_add_reloc (s
, segment
, R_386_GOTPC
);
821 } else if (wrt
== elf_gotoff_sect
+1) {
822 elf_add_reloc (s
, segment
, R_386_GOTOFF
);
823 } else if (wrt
== elf_got_sect
+1) {
824 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
826 } else if (wrt
== elf_sym_sect
+1) {
827 if ( realbytes
== 2 ) {
829 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
832 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
835 } else if (wrt
== elf_plt_sect
+1) {
836 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
837 "relative PLT references");
839 error (ERR_NONFATAL
, "ELF format does not support this"
841 wrt
= NO_SEG
; /* we can at least _try_ to continue */
847 error(ERR_WARNING
|ERR_WARN_GNUELF
,
848 "16-bit relocations in ELF is a GNU extension");
849 WRITESHORT (p
, addr
);
851 if (realbytes
!= 4 && segment
!= NO_SEG
) {
852 error (ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
856 elf_sect_write (s
, mydata
, realbytes
);
857 } else if (type
== OUT_REL2ADR
) {
858 if (segment
== segto
)
859 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
860 if (segment
!= NO_SEG
&& segment
% 2) {
861 error(ERR_NONFATAL
, "ELF format does not support"
862 " segment base references");
865 error (ERR_WARNING
|ERR_WARN_GNUELF
,
866 "16-bit relocations in ELF is a GNU extension");
867 elf_add_reloc (s
, segment
, R_386_PC16
);
869 error (ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
873 WRITESHORT (p
, *(long*)data
- realbytes
);
874 elf_sect_write (s
, mydata
, 2L);
875 } else if (type
== OUT_REL4ADR
) {
876 if (segment
== segto
)
877 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
878 if (segment
!= NO_SEG
&& segment
% 2) {
879 error(ERR_NONFATAL
, "ELF format does not support"
880 " segment base references");
883 elf_add_reloc (s
, segment
, R_386_PC32
);
884 } else if (wrt
== elf_plt_sect
+1) {
885 elf_add_reloc (s
, segment
, R_386_PLT32
);
886 } else if (wrt
== elf_gotpc_sect
+1 ||
887 wrt
== elf_gotoff_sect
+1 ||
888 wrt
== elf_got_sect
+1) {
889 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
890 "relative GOT references");
892 error (ERR_NONFATAL
, "ELF format does not support this"
894 wrt
= NO_SEG
; /* we can at least _try_ to continue */
898 WRITELONG (p
, *(long*)data
- realbytes
);
899 elf_sect_write (s
, mydata
, 4L);
903 static void elf_write(void)
905 int nsections
, align
;
913 long symtablen
, symtablocal
;
916 * Work out how many sections we will have. We have SHN_UNDEF,
917 * then the flexible user sections, then the four fixed
918 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
919 * then optionally relocation sections for the user sections.
921 if (of_elf
.current_dfmt
== &df_stabs
)
924 nsections
= 5; /* SHN_UNDEF and the fixed ones */
926 add_sectname ("", ".comment");
927 add_sectname ("", ".shstrtab");
928 add_sectname ("", ".symtab");
929 add_sectname ("", ".strtab");
930 for (i
=0; i
<nsects
; i
++) {
931 nsections
++; /* for the section itself */
932 if (sects
[i
]->head
) {
933 nsections
++; /* for its relocations */
934 add_sectname (".rel", sects
[i
]->name
);
938 if (of_elf
.current_dfmt
== &df_stabs
) {
939 /* in case the debug information is wanted, just add these three sections... */
940 add_sectname("",".stab");
941 add_sectname("",".stabstr");
942 add_sectname(".rel",".stab");
949 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
952 * Output the ELF header.
954 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
955 fwriteshort (1, elffp
); /* ET_REL relocatable file */
956 fwriteshort (3, elffp
); /* EM_386 processor ID */
957 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
958 fwritelong (0L, elffp
); /* no entry point */
959 fwritelong (0L, elffp
); /* no program header table */
960 fwritelong (0x40L
, elffp
); /* section headers straight after
961 * ELF header plus alignment */
962 fwritelong (0L, elffp
); /* 386 defines no special flags */
963 fwriteshort (0x34, elffp
); /* size of ELF header */
964 fwriteshort (0, elffp
); /* no program header table, again */
965 fwriteshort (0, elffp
); /* still no program header table */
966 fwriteshort (0x28, elffp
); /* size of section header */
967 fwriteshort (nsections
, elffp
); /* number of sections */
968 fwriteshort (nsects
+2, elffp
); /* string table section index for
969 * section header table */
970 fwritelong (0L, elffp
); /* align to 0x40 bytes */
971 fwritelong (0L, elffp
);
972 fwritelong (0L, elffp
);
975 * Build the symbol table and relocation tables.
977 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
978 for (i
=0; i
<nsects
; i
++)
980 sects
[i
]->rel
= elf_build_reltab (§s
[i
]->rellen
,
984 * Now output the section header table.
987 elf_foffs
= 0x40 + 0x28 * nsections
;
988 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
991 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
993 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
994 scount
=1; /* needed for the stabs debugging to track the symtable section */
996 for (i
=0; i
<nsects
; i
++) {
997 elf_section_header (p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
998 (sects
[i
]->type
== SHT_PROGBITS
?
999 sects
[i
]->data
: NULL
), TRUE
,
1000 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1002 scount
++; /* dito */
1004 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
1005 (long)commlen
, 0, 0, 1, 0);/* .comment */
1006 scount
++; /* dito */
1008 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
1009 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
1010 scount
++; /* dito */
1012 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
1013 symtablen
, nsects
+4, symtablocal
, 4, 16);/* .symtab */
1014 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1016 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
1017 strslen
, 0, 0, 1, 0); /* .strtab */
1018 for (i
=0; i
<nsects
; i
++) if (sects
[i
]->head
) {
1020 elf_section_header (p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
1021 sects
[i
]->rellen
, nsects
+3, i
+1, 4, 8);
1023 if (of_elf
.current_dfmt
== &df_stabs
) {
1024 /* for debugging information, create the last three sections
1025 which are the .stab , .stabstr and .rel.stab sections respectively */
1027 /* this function call creates the stab sections in memory */
1030 if ((stabbuf
)&&(stabstrbuf
)&&(stabrelbuf
)) {
1032 elf_section_header(p
-shstrtab
,1,0,stabbuf
,0,stablen
,nsections
-2,0,4,12);
1035 elf_section_header(p
-shstrtab
,3,0,stabstrbuf
,0,stabstrlen
,0,0,4,0);
1038 /* link -> symtable info -> section to refer to */
1039 elf_section_header(p
-shstrtab
,9,0,stabrelbuf
,0,stabrellen
,symtabsection
,nsections
-3,4,8);
1042 fwrite (align_str
, align
, 1, elffp
);
1045 * Now output the sections.
1047 elf_write_sections();
1049 nasm_free (elf_sects
);
1053 static struct SAA
*elf_build_symtab (long *len
, long *local
)
1055 struct SAA
*s
= saa_init(1L);
1057 unsigned char entry
[16], *p
;
1063 * First, an all-zeros entry, required by the ELF spec.
1065 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
1070 * Next, an entry for the file name.
1073 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
1074 WRITELONG (p
, 0); /* no value */
1075 WRITELONG (p
, 0); /* no size either */
1076 WRITESHORT (p
, 4); /* type FILE */
1077 WRITESHORT (p
, SHN_ABS
);
1078 saa_wbytes (s
, entry
, 16L);
1083 * Now some standard symbols defining the segments, for relocation
1086 for (i
= 1; i
<= nsects
+1; i
++) {
1088 WRITELONG (p
, 0); /* no symbol name */
1089 WRITELONG (p
, 0); /* offset zero */
1090 WRITELONG (p
, 0); /* size zero */
1091 WRITESHORT (p
, 3); /* local section-type thing */
1092 WRITESHORT (p
, (i
==1 ? SHN_ABS
: i
-1)); /* the section id */
1093 saa_wbytes (s
, entry
, 16L);
1099 * Now the other local symbols.
1102 while ( (sym
= saa_rstruct (syms
)) ) {
1103 if (sym
->type
& SYM_GLOBAL
)
1106 WRITELONG (p
, sym
->strpos
);
1107 WRITELONG (p
, sym
->value
);
1108 WRITELONG (p
, sym
->size
);
1109 WRITESHORT (p
, sym
->type
); /* local non-typed thing */
1110 WRITESHORT (p
, sym
->section
);
1111 saa_wbytes (s
, entry
, 16L);
1117 * Now the global symbols.
1120 while ( (sym
= saa_rstruct (syms
)) ) {
1121 if (!(sym
->type
& SYM_GLOBAL
))
1124 WRITELONG (p
, sym
->strpos
);
1125 WRITELONG (p
, sym
->value
);
1126 WRITELONG (p
, sym
->size
);
1127 WRITESHORT (p
, sym
->type
); /* global non-typed thing */
1128 WRITESHORT (p
, sym
->section
);
1129 saa_wbytes (s
, entry
, 16L);
1136 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
1138 unsigned char *p
, entry
[8];
1147 long sym
= r
->symbol
;
1149 if (sym
>= GLOBAL_TEMP_BASE
)
1150 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+3) + nlocals
;
1153 WRITELONG (p
, r
->address
);
1154 WRITELONG (p
, (sym
<< 8) + r
->type
);
1155 saa_wbytes (s
, entry
, 8L);
1164 static void elf_section_header (int name
, int type
, int flags
,
1165 void *data
, int is_saa
, long datalen
,
1166 int link
, int info
, int align
, int eltsize
)
1168 elf_sects
[elf_nsect
].data
= data
;
1169 elf_sects
[elf_nsect
].len
= datalen
;
1170 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1173 fwritelong ((long)name
, elffp
);
1174 fwritelong ((long)type
, elffp
);
1175 fwritelong ((long)flags
, elffp
);
1176 fwritelong (0L, elffp
); /* no address, ever, in object files */
1177 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
1178 fwritelong (datalen
, elffp
);
1180 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1181 fwritelong ((long)link
, elffp
);
1182 fwritelong ((long)info
, elffp
);
1183 fwritelong ((long)align
, elffp
);
1184 fwritelong ((long)eltsize
, elffp
);
1187 static void elf_write_sections (void)
1190 for (i
= 0; i
< elf_nsect
; i
++)
1191 if (elf_sects
[i
].data
) {
1192 long len
= elf_sects
[i
].len
;
1193 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1194 long align
= reallen
- len
;
1195 if (elf_sects
[i
].is_saa
)
1196 saa_fpwrite (elf_sects
[i
].data
, elffp
);
1198 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
1199 fwrite (align_str
, align
, 1, elffp
);
1203 static void elf_sect_write (struct Section
*sect
,
1204 const unsigned char *data
, unsigned long len
)
1206 saa_wbytes (sect
->data
, data
, len
);
1210 static long elf_segbase (long segment
)
1215 static int elf_directive (char *directive
, char *value
, int pass
)
1220 static void elf_filename (char *inname
, char *outname
, efunc error
)
1222 strcpy(elf_module
, inname
);
1223 standard_extension (inname
, outname
, ".o", error
);
1226 static const char *elf_stdmac
[] = {
1227 "%define __SECT__ [section .text]",
1228 "%macro __NASM_CDecl__ 1",
1233 static int elf_set_info(enum geninfo type
, char **val
)
1238 static struct dfmt df_stabs
= {
1239 "ELF32 (i386) stabs debug format for Linux",
1250 struct dfmt
*elf_debugs_arr
[2]={&df_stabs
,NULL
};
1252 struct ofmt of_elf
= {
1253 "ELF32 (i386) object files (e.g. Linux)",
1270 /* again, the stabs debugging stuff (code) */
1272 void stabs_init(struct ofmt
*of
,void *id
,FILE *fp
,efunc error
)
1276 void stabs_linenum(const char *filename
,long linenumber
,long segto
)
1278 if (!stabs_filename
) {
1279 stabs_filename
= (char *)nasm_malloc(strlen(filename
)+1);
1280 strcpy(stabs_filename
,filename
);
1282 if (strcmp(stabs_filename
,filename
)) {
1283 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1284 in fact, this leak comes in quite handy to maintain a list of files
1285 encountered so far in the symbol lines... */
1287 /* why not nasm_free(stabs_filename); we're done with the old one */
1289 stabs_filename
= (char *)nasm_malloc(strlen(filename
)+1);
1290 strcpy(stabs_filename
,filename
);
1294 currentline
=linenumber
;
1297 void stabs_deflabel(char *name
,long segment
,long offset
,int is_global
,char *special
)
1301 void stabs_directive(const char *directive
,const char *params
)
1305 void stabs_typevalue(long type
)
1309 void stabs_output(int type
,void *param
)
1311 struct symlininfo
*s
;
1312 struct linelist
*el
;
1313 if (type
==TY_STABSSYMLIN
) {
1314 if (stabs_immcall
) {
1315 s
=(struct symlininfo
*)param
;
1316 if (strcmp( s
->name
,".text")) return; /* we are only interested in the text stuff */
1318 el
=(struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1319 el
->info
.offset
=s
->offset
;
1320 el
->info
.section
=s
->section
;
1321 el
->info
.name
=s
->name
;
1322 el
->line
=currentline
;
1323 el
->filename
=stabs_filename
;
1326 stabslines
->last
->next
=el
;
1327 stabslines
->last
=el
;
1330 stabslines
->last
=el
;
1337 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1339 WRITELONG(p,n_strx); \
1340 WRITECHAR(p,n_type); \
1341 WRITECHAR(p,n_other); \
1342 WRITESHORT(p,n_desc); \
1343 WRITELONG(p,n_value); \
1346 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1348 void stabs_generate(void)
1350 int i
,numfiles
,strsize
,numstabs
=0,currfile
,mainfileindex
;
1351 unsigned char *sbuf
,*ssbuf
,*rbuf
,*sptr
,*rptr
;
1355 struct linelist
*ptr
;
1359 allfiles
= (char **)nasm_malloc(numlinestabs
*sizeof(char *));
1360 for (i
=0;i
<numlinestabs
;i
++) allfiles
[i
]=0;
1364 allfiles
[0]=ptr
->filename
;
1367 for (i
=0;i
<numfiles
;i
++) {
1368 if (!strcmp(allfiles
[i
],ptr
->filename
)) break;
1371 allfiles
[i
]=ptr
->filename
;
1378 fileidx
= (int *)nasm_malloc(numfiles
*sizeof(int));
1379 for (i
=0;i
<numfiles
;i
++) {
1381 strsize
+=strlen(allfiles
[i
])+1;
1384 for (i
=0;i
<numfiles
;i
++) {
1385 if (!strcmp(allfiles
[i
],elf_module
)) {
1392 /* worst case size of the stab buffer would be:
1393 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1395 sbuf
= (unsigned char *)nasm_malloc((numlinestabs
*2+3)*sizeof(struct stabentry
));
1397 ssbuf
= (unsigned char *)nasm_malloc(strsize
);
1399 rbuf
= (unsigned char *)nasm_malloc(numlinestabs
*8*(2+3));
1402 for (i
=0;i
<numfiles
;i
++) {
1403 strcpy((char *)ssbuf
+fileidx
[i
],allfiles
[i
]);
1407 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1410 /* this is the first stab, its strx points to the filename of the
1411 the source-file, the n_desc field should be set to the number
1414 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0]+12));
1420 /* this is the stab for the main source file */
1421 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1423 /* relocation stuff */
1424 /* IS THIS SANE? WHAT DOES SECTION+3 MEAN HERE? */
1425 WRITELONG(rptr
, (sptr
-sbuf
)-4);
1426 WRITELONG(rptr
, ((ptr
->info
.section
+3)<<8)|R_386_32
);
1429 currfile
=mainfileindex
;
1433 if (strcmp(allfiles
[currfile
],ptr
->filename
)) {
1434 /* oops file has changed... */
1435 for (i
=0;i
<numfiles
;i
++) if (!strcmp(allfiles
[i
],ptr
->filename
)) break;
1437 WRITE_STAB(sptr
,fileidx
[currfile
],N_SOL
,0,0,ptr
->info
.offset
);
1440 /* relocation stuff */
1441 /* IS THIS SANE? WHAT DOES SECTION+3 MEAN HERE? */
1442 WRITELONG(rptr
, (sptr
-sbuf
)-4);
1443 WRITELONG(rptr
, ((ptr
->info
.section
+3)<<8)|R_386_32
);
1446 WRITE_STAB(sptr
,0,N_SLINE
,0,ptr
->line
,ptr
->info
.offset
);
1449 /* relocation stuff */
1450 /* IS THIS SANE? WHAT DOES SECTION+3 MEAN HERE? */
1451 WRITELONG(rptr
, (sptr
-sbuf
)-4);
1452 WRITELONG(rptr
, ((ptr
->info
.section
+3)<<8)|R_386_32
);
1459 ((struct stabentry
*)sbuf
)->n_desc
=numstabs
;
1461 nasm_free(allfiles
);
1464 stablen
= (sptr
-sbuf
);
1465 stabrellen
=(rptr
-rbuf
);
1471 void stabs_cleanup() {
1472 struct linelist
*ptr
,*del
;
1473 if (!stabslines
) return;
1480 if (stabbuf
) nasm_free(stabbuf
);
1481 if (stabrelbuf
) nasm_free(stabrelbuf
);
1482 if (stabstrbuf
) nasm_free(stabstrbuf
);