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.
29 R_386_32
= 1, /* ordinary absolute relocation */
30 R_386_PC32
= 2, /* PC-relative relocation */
31 R_386_GOT32
= 3, /* an offset into GOT */
32 R_386_PLT32
= 4, /* a PC-relative offset into PLT */
33 R_386_COPY
= 5, /* ??? */
34 R_386_GLOB_DAT
= 6, /* ??? */
35 R_386_JUMP_SLOT
= 7, /* ??? */
36 R_386_RELATIVE
= 8, /* ??? */
37 R_386_GOTOFF
= 9, /* an offset from GOT base */
38 R_386_GOTPC
= 10, /* a PC-relative offset _to_ GOT */
39 /* These are GNU extensions, but useful */
40 R_386_16
= 20, /* A 16-bit absolute relocation */
41 R_386_PC16
= 21, /* A 16-bit PC-relative relocation */
42 R_386_8
= 22, /* An 8-bit absolute relocation */
43 R_386_PC8
= 23 /* An 8-bit PC-relative relocation */
48 int32_t address
; /* relative to _start_ of section */
49 int32_t symbol
; /* ELF symbol info thingy */
50 int type
; /* type of relocation */
54 int32_t strpos
; /* string table position of name */
55 int32_t section
; /* section ID of the symbol */
56 int type
; /* symbol type */
57 int other
; /* symbol visibility */
58 int32_t value
; /* address, or COMMON variable align */
59 int32_t size
; /* size of symbol */
60 int32_t globnum
; /* symbol table offset if global */
61 struct Symbol
*next
; /* list of globals in each section */
62 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
63 char *name
; /* used temporarily if in above list */
66 #define SHT_PROGBITS 1
71 #define SHF_EXECINSTR 4
75 uint32_t len
, size
, nrelocs
;
77 int type
; /* SHT_PROGBITS or SHT_NOBITS */
78 int align
; /* alignment: power of two */
79 uint32_t flags
; /* section flags */
83 struct Reloc
*head
, **tail
;
84 struct Symbol
*gsyms
; /* global symbols in section */
88 static struct Section
**sects
;
89 static int nsects
, sectlen
;
91 #define SHSTR_DELTA 256
92 static char *shstrtab
;
93 static int shstrtablen
, shstrtabsize
;
95 static struct SAA
*syms
;
96 static uint32_t nlocals
, nglobs
;
98 static int32_t def_seg
;
100 static struct RAA
*bsym
;
102 static struct SAA
*strs
;
103 static uint32_t strslen
;
107 static evalfunc evaluate
;
109 static struct Symbol
*fwds
;
111 static char elf_module
[FILENAME_MAX
];
113 extern struct ofmt of_elf32
;
114 extern struct ofmt of_elf
;
116 #define SHN_ABS 0xFFF1
117 #define SHN_COMMON 0xFFF2
120 #define SYM_SECTION 0x04
121 #define SYM_GLOBAL 0x10
122 #define SYM_NOTYPE 0x00
123 #define SYM_DATA 0x01
124 #define SYM_FUNCTION 0x02
126 #define STV_DEFAULT 0
127 #define STV_INTERNAL 1
129 #define STV_PROTECTED 3
131 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
133 #define SEG_ALIGN 16 /* alignment of sections in file */
134 #define SEG_ALIGN_1 (SEG_ALIGN-1)
136 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
138 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
139 static struct ELF_SECTDATA
{
144 static int elf_nsect
;
145 static int32_t elf_foffs
;
147 static void elf_write(void);
148 static void elf_sect_write(struct Section
*, const uint8_t *,
150 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
152 static void elf_write_sections(void);
153 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
154 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
155 static void add_sectname(char *, char *);
157 /* this stuff is needed for the stabs debugging format */
158 #define N_SO 0x64 /* ID for main source file */
159 #define N_SOL 0x84 /* ID for sub-source file */
163 #define TY_STABSSYMLIN 0x40 /* ouch */
179 int section
; /* section index */
180 char *name
; /* shallow-copied pointer of section name */
184 struct symlininfo info
;
187 struct linelist
*next
;
188 struct linelist
*last
;
191 static struct linelist
*stabslines
= 0;
192 static int stabs_immcall
= 0;
193 static int currentline
= 0;
194 static int numlinestabs
= 0;
195 static char *stabs_filename
= 0;
196 static int symtabsection
;
197 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
198 static int stablen
, stabstrlen
, stabrellen
;
200 static struct dfmt df_stabs
;
202 void stabs32_init(struct ofmt
*, void *, FILE *, efunc
);
203 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t);
204 void stabs32_deflabel(char *, int32_t, int32_t, int, char *);
205 void stabs32_directive(const char *, const char *);
206 void stabs32_typevalue(int32_t);
207 void stabs32_output(int, void *);
208 void stabs32_generate(void);
209 void stabs32_cleanup(void);
211 /* end of stabs debugging stuff */
214 * Special section numbers which are used to define ELF special
215 * symbols, which can be used with WRT to provide PIC relocation
218 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
219 static int32_t elf_got_sect
, elf_plt_sect
;
220 static int32_t elf_sym_sect
;
222 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
227 (void)ldef
; /* placate optimisers */
229 nsects
= sectlen
= 0;
230 syms
= saa_init((int32_t)sizeof(struct Symbol
));
231 nlocals
= nglobs
= 0;
234 saa_wbytes(strs
, "\0", 1L);
235 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
236 strslen
= 2 + strlen(elf_module
);
238 shstrtablen
= shstrtabsize
= 0;;
239 add_sectname("", "");
243 elf_gotpc_sect
= seg_alloc();
244 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
246 elf_gotoff_sect
= seg_alloc();
247 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
249 elf_got_sect
= seg_alloc();
250 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
252 elf_plt_sect
= seg_alloc();
253 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
255 elf_sym_sect
= seg_alloc();
256 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf32
,
259 def_seg
= seg_alloc();
262 static void elf_cleanup(int debuginfo
)
271 for (i
= 0; i
< nsects
; i
++) {
272 if (sects
[i
]->type
!= SHT_NOBITS
)
273 saa_free(sects
[i
]->data
);
275 saa_free(sects
[i
]->rel
);
276 while (sects
[i
]->head
) {
278 sects
[i
]->head
= sects
[i
]->head
->next
;
286 if (of_elf32
.current_dfmt
) {
287 of_elf32
.current_dfmt
->cleanup();
291 static void add_sectname(char *firsthalf
, char *secondhalf
)
293 int len
= strlen(firsthalf
) + strlen(secondhalf
);
294 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
295 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
296 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
297 strcat(shstrtab
+ shstrtablen
, secondhalf
);
298 shstrtablen
+= len
+ 1;
301 static int elf_make_section(char *name
, int type
, int flags
, int align
)
305 s
= nasm_malloc(sizeof(*s
));
307 if (type
!= SHT_NOBITS
)
308 s
->data
= saa_init(1L);
311 s
->len
= s
->size
= 0;
313 if (!strcmp(name
, ".text"))
316 s
->index
= seg_alloc();
317 add_sectname("", name
);
318 s
->name
= nasm_malloc(1 + strlen(name
));
319 strcpy(s
->name
, name
);
325 if (nsects
>= sectlen
)
327 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
333 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
336 int flags_and
, flags_or
, type
, align
, i
;
339 * Default is 32 bits.
347 while (*p
&& !isspace(*p
))
351 flags_and
= flags_or
= type
= align
= 0;
353 while (*p
&& isspace(*p
))
357 while (*p
&& !isspace(*p
))
361 while (*p
&& isspace(*p
))
364 if (!nasm_strnicmp(q
, "align=", 6)) {
368 if ((align
- 1) & align
) { /* means it's not a power of two */
369 error(ERR_NONFATAL
, "section alignment %d is not"
370 " a power of two", align
);
373 } else if (!nasm_stricmp(q
, "alloc")) {
374 flags_and
|= SHF_ALLOC
;
375 flags_or
|= SHF_ALLOC
;
376 } else if (!nasm_stricmp(q
, "noalloc")) {
377 flags_and
|= SHF_ALLOC
;
378 flags_or
&= ~SHF_ALLOC
;
379 } else if (!nasm_stricmp(q
, "exec")) {
380 flags_and
|= SHF_EXECINSTR
;
381 flags_or
|= SHF_EXECINSTR
;
382 } else if (!nasm_stricmp(q
, "noexec")) {
383 flags_and
|= SHF_EXECINSTR
;
384 flags_or
&= ~SHF_EXECINSTR
;
385 } else if (!nasm_stricmp(q
, "write")) {
386 flags_and
|= SHF_WRITE
;
387 flags_or
|= SHF_WRITE
;
388 } else if (!nasm_stricmp(q
, "nowrite")) {
389 flags_and
|= SHF_WRITE
;
390 flags_or
&= ~SHF_WRITE
;
391 } else if (!nasm_stricmp(q
, "progbits")) {
393 } else if (!nasm_stricmp(q
, "nobits")) {
398 if (!strcmp(name
, ".comment") ||
399 !strcmp(name
, ".shstrtab") ||
400 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
401 error(ERR_NONFATAL
, "attempt to redefine reserved section"
406 for (i
= 0; i
< nsects
; i
++)
407 if (!strcmp(name
, sects
[i
]->name
))
410 if (!strcmp(name
, ".text"))
411 i
= elf_make_section(name
, SHT_PROGBITS
,
412 SHF_ALLOC
| SHF_EXECINSTR
, 16);
413 else if (!strcmp(name
, ".rodata"))
414 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
415 else if (!strcmp(name
, ".data"))
416 i
= elf_make_section(name
, SHT_PROGBITS
,
417 SHF_ALLOC
| SHF_WRITE
, 4);
418 else if (!strcmp(name
, ".bss"))
419 i
= elf_make_section(name
, SHT_NOBITS
,
420 SHF_ALLOC
| SHF_WRITE
, 4);
422 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
424 sects
[i
]->type
= type
;
426 sects
[i
]->align
= align
;
427 sects
[i
]->flags
&= ~flags_and
;
428 sects
[i
]->flags
|= flags_or
;
429 } else if (pass
== 1) {
430 if (type
|| align
|| flags_and
)
431 error(ERR_WARNING
, "section attributes ignored on"
432 " redeclaration of section `%s'", name
);
435 return sects
[i
]->index
;
438 static void elf_deflabel(char *name
, int32_t segment
, int32_t offset
,
439 int is_global
, char *special
)
443 bool special_used
= false;
445 #if defined(DEBUG) && DEBUG>2
447 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
448 name
, segment
, offset
, is_global
, special
);
450 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
452 * This is a NASM special symbol. We never allow it into
453 * the ELF symbol table, even if it's a valid one. If it
454 * _isn't_ a valid one, we should barf immediately.
456 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
457 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
458 strcmp(name
, "..sym"))
459 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
463 if (is_global
== 3) {
466 * Fix up a forward-reference symbol size from the first
469 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
470 if (!strcmp((*s
)->name
, name
)) {
471 struct tokenval tokval
;
475 while (*p
&& !isspace(*p
))
477 while (*p
&& isspace(*p
))
481 tokval
.t_type
= TOKEN_INVALID
;
482 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
485 error(ERR_NONFATAL
, "cannot use relocatable"
486 " expression as symbol size");
488 (*s
)->size
= reloc_value(e
);
492 * Remove it from the list of unresolved sizes.
494 nasm_free((*s
)->name
);
498 return; /* it wasn't an important one */
501 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
502 strslen
+= 1 + strlen(name
);
504 sym
= saa_wstruct(syms
);
507 sym
->type
= is_global
? SYM_GLOBAL
: 0;
508 sym
->other
= STV_DEFAULT
;
510 if (segment
== NO_SEG
)
511 sym
->section
= SHN_ABS
;
514 sym
->section
= SHN_UNDEF
;
515 if (nsects
== 0 && segment
== def_seg
) {
517 if (segment
!= elf_section_names(".text", 2, &tempint
))
519 "strange segment conditions in ELF driver");
520 sym
->section
= nsects
;
522 for (i
= 0; i
< nsects
; i
++)
523 if (segment
== sects
[i
]->index
) {
524 sym
->section
= i
+ 1;
530 if (is_global
== 2) {
533 sym
->section
= SHN_COMMON
;
535 * We have a common variable. Check the special text to see
536 * if it's a valid number and power of two; if so, store it
537 * as the alignment for the common variable.
541 sym
->value
= readnum(special
, &err
);
543 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
544 " valid number", special
);
545 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
546 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
547 " power of two", special
);
551 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
553 if (sym
->type
== SYM_GLOBAL
) {
555 * If sym->section == SHN_ABS, then the first line of the
556 * else section would cause a core dump, because its a reference
557 * beyond the end of the section array.
558 * This behaviour is exhibited by this code:
561 * To avoid such a crash, such requests are silently discarded.
562 * This may not be the best solution.
564 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
565 bsym
= raa_write(bsym
, segment
, nglobs
);
566 } else if (sym
->section
!= SHN_ABS
) {
568 * This is a global symbol; so we must add it to the linked
569 * list of global symbols in its section. We'll push it on
570 * the beginning of the list, because it doesn't matter
571 * much which end we put it on and it's easier like this.
573 * In addition, we check the special text for symbol
574 * type and size information.
576 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
577 sects
[sym
->section
- 1]->gsyms
= sym
;
580 int n
= strcspn(special
, " \t");
582 if (!nasm_strnicmp(special
, "function", n
))
583 sym
->type
|= SYM_FUNCTION
;
584 else if (!nasm_strnicmp(special
, "data", n
) ||
585 !nasm_strnicmp(special
, "object", n
))
586 sym
->type
|= SYM_DATA
;
587 else if (!nasm_strnicmp(special
, "notype", n
))
588 sym
->type
|= SYM_NOTYPE
;
590 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
594 while (isspace(*special
))
597 n
= strcspn(special
, " \t");
598 if (!nasm_strnicmp(special
, "default", n
))
599 sym
->other
= STV_DEFAULT
;
600 else if (!nasm_strnicmp(special
, "internal", n
))
601 sym
->other
= STV_INTERNAL
;
602 else if (!nasm_strnicmp(special
, "hidden", n
))
603 sym
->other
= STV_HIDDEN
;
604 else if (!nasm_strnicmp(special
, "protected", n
))
605 sym
->other
= STV_PROTECTED
;
612 struct tokenval tokval
;
615 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
617 while (special
[n
] && isspace(special
[n
]))
620 * We have a size expression; attempt to
624 stdscan_bufptr
= special
+ n
;
625 tokval
.t_type
= TOKEN_INVALID
;
626 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
631 sym
->name
= nasm_strdup(name
);
634 error(ERR_NONFATAL
, "cannot use relocatable"
635 " expression as symbol size");
637 sym
->size
= reloc_value(e
);
639 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
644 sym
->globnum
= nglobs
;
649 if (special
&& !special_used
)
650 error(ERR_NONFATAL
, "no special symbol features supported here");
653 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
657 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
658 sect
->tail
= &r
->next
;
661 r
->address
= sect
->len
;
662 if (segment
== NO_SEG
)
667 for (i
= 0; i
< nsects
; i
++)
668 if (segment
== sects
[i
]->index
)
671 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
679 * This routine deals with ..got and ..sym relocations: the more
680 * complicated kinds. In shared-library writing, some relocations
681 * with respect to global symbols must refer to the precise symbol
682 * rather than referring to an offset from the base of the section
683 * _containing_ the symbol. Such relocations call to this routine,
684 * which searches the symbol list for the symbol in question.
686 * R_386_GOT32 references require the _exact_ symbol address to be
687 * used; R_386_32 references can be at an offset from the symbol.
688 * The boolean argument `exact' tells us this.
690 * Return value is the adjusted value of `addr', having become an
691 * offset from the symbol rather than the section. Should always be
692 * zero when returning from an exact call.
694 * Limitation: if you define two symbols at the same place,
695 * confusion will occur.
697 * Inefficiency: we search, currently, using a linked list which
698 * isn't even necessarily sorted.
700 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
701 int32_t segment
, int32_t offset
,
702 int type
, bool exact
)
706 struct Symbol
*sym
, *sm
;
710 * First look up the segment/offset pair and find a global
711 * symbol corresponding to it. If it's not one of our segments,
712 * then it must be an external symbol, in which case we're fine
713 * doing a normal elf_add_reloc after first sanity-checking
714 * that the offset from the symbol is zero.
717 for (i
= 0; i
< nsects
; i
++)
718 if (segment
== sects
[i
]->index
) {
723 if (exact
&& offset
!= 0)
724 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
725 " for this reference");
727 elf_add_reloc(sect
, segment
, type
);
733 * Find a symbol pointing _exactly_ at this one.
735 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
736 if (sym
->value
== offset
)
740 * Find the nearest symbol below this one.
743 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
744 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
748 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
749 " for this reference");
753 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
754 sect
->tail
= &r
->next
;
757 r
->address
= sect
->len
;
758 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
763 return offset
- sym
->value
;
766 static void elf_out(int32_t segto
, const void *data
, uint32_t type
,
767 int32_t segment
, int32_t wrt
)
770 int32_t realbytes
= type
& OUT_SIZMASK
;
772 uint8_t mydata
[4], *p
;
774 static struct symlininfo sinfo
;
779 * handle absolute-assembly (structure definitions)
781 if (segto
== NO_SEG
) {
782 if (type
!= OUT_RESERVE
)
783 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
789 for (i
= 0; i
< nsects
; i
++)
790 if (segto
== sects
[i
]->index
) {
795 int tempint
; /* ignored */
796 if (segto
!= elf_section_names(".text", 2, &tempint
))
797 error(ERR_PANIC
, "strange segment conditions in ELF driver");
799 s
= sects
[nsects
- 1];
804 /* again some stabs debugging stuff */
805 if (of_elf32
.current_dfmt
) {
806 sinfo
.offset
= s
->len
;
808 sinfo
.name
= s
->name
;
809 of_elf32
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
811 /* end of debugging stuff */
813 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
814 error(ERR_WARNING
, "attempt to initialize memory in"
815 " BSS section `%s': ignored", s
->name
);
816 if (type
== OUT_REL2ADR
)
818 else if (type
== OUT_REL4ADR
)
824 if (type
== OUT_RESERVE
) {
825 if (s
->type
== SHT_PROGBITS
) {
826 error(ERR_WARNING
, "uninitialized space declared in"
827 " non-BSS section `%s': zeroing", s
->name
);
828 elf_sect_write(s
, NULL
, realbytes
);
831 } else if (type
== OUT_RAWDATA
) {
832 if (segment
!= NO_SEG
)
833 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
834 elf_sect_write(s
, data
, realbytes
);
835 } else if (type
== OUT_ADDRESS
) {
837 addr
= *(int32_t *)data
;
838 if (segment
!= NO_SEG
) {
840 error(ERR_NONFATAL
, "ELF format does not support"
841 " segment base references");
844 if (realbytes
== 2) {
846 elf_add_reloc(s
, segment
, R_386_16
);
848 elf_add_reloc(s
, segment
, R_386_32
);
850 } else if (wrt
== elf_gotpc_sect
+ 1) {
852 * The user will supply GOT relative to $$. ELF
853 * will let us have GOT relative to $. So we
854 * need to fix up the data item by $-$$.
857 elf_add_reloc(s
, segment
, R_386_GOTPC
);
858 } else if (wrt
== elf_gotoff_sect
+ 1) {
859 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
860 } else if (wrt
== elf_got_sect
+ 1) {
861 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
863 } else if (wrt
== elf_sym_sect
+ 1) {
864 if (realbytes
== 2) {
866 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
869 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
872 } else if (wrt
== elf_plt_sect
+ 1) {
873 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
874 "relative PLT references");
876 error(ERR_NONFATAL
, "ELF format does not support this"
878 wrt
= NO_SEG
; /* we can at least _try_ to continue */
884 error(ERR_WARNING
| ERR_WARN_GNUELF
,
885 "16-bit relocations in ELF is a GNU extension");
888 if (realbytes
!= 4 && segment
!= NO_SEG
) {
890 "Unsupported non-32-bit ELF relocation");
894 elf_sect_write(s
, mydata
, realbytes
);
895 } else if (type
== OUT_REL2ADR
) {
896 if (segment
== segto
)
897 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
898 if (segment
!= NO_SEG
&& segment
% 2) {
899 error(ERR_NONFATAL
, "ELF format does not support"
900 " segment base references");
903 error(ERR_WARNING
| ERR_WARN_GNUELF
,
904 "16-bit relocations in ELF is a GNU extension");
905 elf_add_reloc(s
, segment
, R_386_PC16
);
908 "Unsupported non-32-bit ELF relocation");
912 WRITESHORT(p
, *(int32_t *)data
- realbytes
);
913 elf_sect_write(s
, mydata
, 2L);
914 } else if (type
== OUT_REL4ADR
) {
915 if (segment
== segto
)
916 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
917 if (segment
!= NO_SEG
&& segment
% 2) {
918 error(ERR_NONFATAL
, "ELF format does not support"
919 " segment base references");
922 elf_add_reloc(s
, segment
, R_386_PC32
);
923 } else if (wrt
== elf_plt_sect
+ 1) {
924 elf_add_reloc(s
, segment
, R_386_PLT32
);
925 } else if (wrt
== elf_gotpc_sect
+ 1 ||
926 wrt
== elf_gotoff_sect
+ 1 ||
927 wrt
== elf_got_sect
+ 1) {
928 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
929 "relative GOT references");
931 error(ERR_NONFATAL
, "ELF format does not support this"
933 wrt
= NO_SEG
; /* we can at least _try_ to continue */
937 WRITELONG(p
, *(int32_t *)data
- realbytes
);
938 elf_sect_write(s
, mydata
, 4L);
942 static void elf_write(void)
944 int nsections
, align
;
952 int32_t symtablen
, symtablocal
;
955 * Work out how many sections we will have. We have SHN_UNDEF,
956 * then the flexible user sections, then the four fixed
957 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
958 * then optionally relocation sections for the user sections.
960 if (of_elf32
.current_dfmt
== &df_stabs
)
963 nsections
= 5; /* SHN_UNDEF and the fixed ones */
965 add_sectname("", ".comment");
966 add_sectname("", ".shstrtab");
967 add_sectname("", ".symtab");
968 add_sectname("", ".strtab");
969 for (i
= 0; i
< nsects
; i
++) {
970 nsections
++; /* for the section itself */
971 if (sects
[i
]->head
) {
972 nsections
++; /* for its relocations */
973 add_sectname(".rel", sects
[i
]->name
);
977 if (of_elf32
.current_dfmt
== &df_stabs
) {
978 /* in case the debug information is wanted, just add these three sections... */
979 add_sectname("", ".stab");
980 add_sectname("", ".stabstr");
981 add_sectname(".rel", ".stab");
989 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
992 * Output the ELF header.
994 fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
995 fwriteint16_t(1, elffp
); /* ET_REL relocatable file */
996 fwriteint16_t(3, elffp
); /* EM_386 processor ID */
997 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
998 fwriteint32_t(0L, elffp
); /* no entry point */
999 fwriteint32_t(0L, elffp
); /* no program header table */
1000 fwriteint32_t(0x40L
, elffp
); /* section headers straight after
1001 * ELF header plus alignment */
1002 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1003 fwriteint16_t(0x34, elffp
); /* size of ELF header */
1004 fwriteint16_t(0, elffp
); /* no program header table, again */
1005 fwriteint16_t(0, elffp
); /* still no program header table */
1006 fwriteint16_t(0x28, elffp
); /* size of section header */
1007 fwriteint16_t(nsections
, elffp
); /* number of sections */
1008 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1009 * section header table */
1010 fwriteint32_t(0L, elffp
); /* align to 0x40 bytes */
1011 fwriteint32_t(0L, elffp
);
1012 fwriteint32_t(0L, elffp
);
1015 * Build the symbol table and relocation tables.
1017 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1018 for (i
= 0; i
< nsects
; i
++)
1020 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1024 * Now output the section header table.
1027 elf_foffs
= 0x40 + 0x28 * nsections
;
1028 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1031 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1033 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1034 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1036 for (i
= 0; i
< nsects
; i
++) {
1037 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1038 (sects
[i
]->type
== SHT_PROGBITS
?
1039 sects
[i
]->data
: NULL
), true,
1040 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1042 scount
++; /* dito */
1044 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1045 scount
++; /* dito */
1047 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1048 scount
++; /* dito */
1050 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 16); /* .symtab */
1051 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1053 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1054 for (i
= 0; i
< nsects
; i
++)
1055 if (sects
[i
]->head
) {
1057 elf_section_header(p
- shstrtab
, 9, 0, sects
[i
]->rel
, true,
1058 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 8);
1060 if (of_elf32
.current_dfmt
== &df_stabs
) {
1061 /* for debugging information, create the last three sections
1062 which are the .stab , .stabstr and .rel.stab sections respectively */
1064 /* this function call creates the stab sections in memory */
1067 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1069 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1070 nsections
- 2, 0, 4, 12);
1073 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1074 stabstrlen
, 0, 0, 4, 0);
1077 /* link -> symtable info -> section to refer to */
1078 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1079 stabrellen
, symtabsection
, nsections
- 3, 4,
1083 fwrite(align_str
, align
, 1, elffp
);
1086 * Now output the sections.
1088 elf_write_sections();
1090 nasm_free(elf_sects
);
1094 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1096 struct SAA
*s
= saa_init(1L);
1098 uint8_t entry
[16], *p
;
1104 * First, an all-zeros entry, required by the ELF spec.
1106 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1111 * Next, an entry for the file name.
1114 WRITELONG(p
, 1); /* we know it's 1st thing in strtab */
1115 WRITELONG(p
, 0); /* no value */
1116 WRITELONG(p
, 0); /* no size either */
1117 WRITESHORT(p
, 4); /* type FILE */
1118 WRITESHORT(p
, SHN_ABS
);
1119 saa_wbytes(s
, entry
, 16L);
1124 * Now some standard symbols defining the segments, for relocation
1127 for (i
= 1; i
<= nsects
+ 1; i
++) {
1129 WRITELONG(p
, 0); /* no symbol name */
1130 WRITELONG(p
, 0); /* offset zero */
1131 WRITELONG(p
, 0); /* size zero */
1132 WRITESHORT(p
, 3); /* local section-type thing */
1133 WRITESHORT(p
, (i
== 1 ? SHN_ABS
: i
- 1)); /* the section id */
1134 saa_wbytes(s
, entry
, 16L);
1140 * Now the other local symbols.
1143 while ((sym
= saa_rstruct(syms
))) {
1144 if (sym
->type
& SYM_GLOBAL
)
1147 WRITELONG(p
, sym
->strpos
);
1148 WRITELONG(p
, sym
->value
);
1149 WRITELONG(p
, sym
->size
);
1150 WRITECHAR(p
, sym
->type
); /* local non-typed thing */
1151 WRITECHAR(p
, sym
->other
);
1152 WRITESHORT(p
, sym
->section
);
1153 saa_wbytes(s
, entry
, 16L);
1159 * Now the global symbols.
1162 while ((sym
= saa_rstruct(syms
))) {
1163 if (!(sym
->type
& SYM_GLOBAL
))
1166 WRITELONG(p
, sym
->strpos
);
1167 WRITELONG(p
, sym
->value
);
1168 WRITELONG(p
, sym
->size
);
1169 WRITECHAR(p
, sym
->type
); /* global non-typed thing */
1170 WRITECHAR(p
, sym
->other
);
1171 WRITESHORT(p
, sym
->section
);
1172 saa_wbytes(s
, entry
, 16L);
1179 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1182 uint8_t *p
, entry
[8];
1191 int32_t sym
= r
->symbol
;
1193 if (sym
>= GLOBAL_TEMP_BASE
)
1194 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 3) + nlocals
;
1197 WRITELONG(p
, r
->address
);
1198 WRITELONG(p
, (sym
<< 8) + r
->type
);
1199 saa_wbytes(s
, entry
, 8L);
1208 static void elf_section_header(int name
, int type
, int flags
,
1209 void *data
, bool is_saa
, int32_t datalen
,
1210 int link
, int info
, int align
, int eltsize
)
1212 elf_sects
[elf_nsect
].data
= data
;
1213 elf_sects
[elf_nsect
].len
= datalen
;
1214 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1217 fwriteint32_t((int32_t)name
, elffp
);
1218 fwriteint32_t((int32_t)type
, elffp
);
1219 fwriteint32_t((int32_t)flags
, elffp
);
1220 fwriteint32_t(0L, elffp
); /* no address, ever, in object files */
1221 fwriteint32_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1222 fwriteint32_t(datalen
, elffp
);
1224 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1225 fwriteint32_t((int32_t)link
, elffp
);
1226 fwriteint32_t((int32_t)info
, elffp
);
1227 fwriteint32_t((int32_t)align
, elffp
);
1228 fwriteint32_t((int32_t)eltsize
, elffp
);
1231 static void elf_write_sections(void)
1234 for (i
= 0; i
< elf_nsect
; i
++)
1235 if (elf_sects
[i
].data
) {
1236 int32_t len
= elf_sects
[i
].len
;
1237 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1238 int32_t align
= reallen
- len
;
1239 if (elf_sects
[i
].is_saa
)
1240 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1242 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1243 fwrite(align_str
, align
, 1, elffp
);
1247 static void elf_sect_write(struct Section
*sect
,
1248 const uint8_t *data
, uint32_t len
)
1250 saa_wbytes(sect
->data
, data
, len
);
1254 static int32_t elf_segbase(int32_t segment
)
1259 static int elf_directive(char *directive
, char *value
, int pass
)
1267 static void elf_filename(char *inname
, char *outname
, efunc error
)
1269 strcpy(elf_module
, inname
);
1270 standard_extension(inname
, outname
, ".o", error
);
1273 static const char *elf_stdmac
[] = {
1274 "%define __SECT__ [section .text]",
1275 "%macro __NASM_CDecl__ 1",
1280 static int elf_set_info(enum geninfo type
, char **val
)
1287 static struct dfmt df_stabs
= {
1288 "ELF32 (i386) stabs debug format for Linux",
1299 struct dfmt
*elf32_debugs_arr
[2] = { &df_stabs
, NULL
};
1301 struct ofmt of_elf32
= {
1302 "ELF32 (i386) object files (e.g. Linux)",
1319 struct ofmt of_elf
= {
1320 "ELF (short name for ELF32) ",
1336 /* again, the stabs debugging stuff (code) */
1338 void stabs32_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1346 void stabs32_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1350 if (!stabs_filename
) {
1351 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1352 strcpy(stabs_filename
, filename
);
1354 if (strcmp(stabs_filename
, filename
)) {
1355 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1356 in fact, this leak comes in quite handy to maintain a list of files
1357 encountered so far in the symbol lines... */
1359 /* why not nasm_free(stabs_filename); we're done with the old one */
1361 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1362 strcpy(stabs_filename
, filename
);
1366 currentline
= linenumber
;
1369 void stabs32_deflabel(char *name
, int32_t segment
, int32_t offset
, int is_global
,
1379 void stabs32_directive(const char *directive
, const char *params
)
1385 void stabs32_typevalue(int32_t type
)
1390 void stabs32_output(int type
, void *param
)
1392 struct symlininfo
*s
;
1393 struct linelist
*el
;
1394 if (type
== TY_STABSSYMLIN
) {
1395 if (stabs_immcall
) {
1396 s
= (struct symlininfo
*)param
;
1397 if (strcmp(s
->name
, ".text"))
1398 return; /* we are only interested in the text stuff */
1400 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1401 el
->info
.offset
= s
->offset
;
1402 el
->info
.section
= s
->section
;
1403 el
->info
.name
= s
->name
;
1404 el
->line
= currentline
;
1405 el
->filename
= stabs_filename
;
1408 stabslines
->last
->next
= el
;
1409 stabslines
->last
= el
;
1412 stabslines
->last
= el
;
1419 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1421 WRITELONG(p,n_strx); \
1422 WRITECHAR(p,n_type); \
1423 WRITECHAR(p,n_other); \
1424 WRITESHORT(p,n_desc); \
1425 WRITELONG(p,n_value); \
1428 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1430 void stabs32_generate(void)
1432 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1433 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1437 struct linelist
*ptr
;
1441 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1442 for (i
= 0; i
< numlinestabs
; i
++)
1446 if (numfiles
== 0) {
1447 allfiles
[0] = ptr
->filename
;
1450 for (i
= 0; i
< numfiles
; i
++) {
1451 if (!strcmp(allfiles
[i
], ptr
->filename
))
1454 if (i
>= numfiles
) {
1455 allfiles
[i
] = ptr
->filename
;
1462 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1463 for (i
= 0; i
< numfiles
; i
++) {
1464 fileidx
[i
] = strsize
;
1465 strsize
+= strlen(allfiles
[i
]) + 1;
1468 for (i
= 0; i
< numfiles
; i
++) {
1469 if (!strcmp(allfiles
[i
], elf_module
)) {
1475 /* worst case size of the stab buffer would be:
1476 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1479 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1480 sizeof(struct stabentry
));
1482 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1484 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1487 for (i
= 0; i
< numfiles
; i
++) {
1488 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1492 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1495 /* this is the first stab, its strx points to the filename of the
1496 the source-file, the n_desc field should be set to the number
1499 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1505 /* this is the stab for the main source file */
1506 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1508 /* relocation table entry */
1510 /* Since the above WRITE_STAB calls have already */
1511 /* created two entries, the index in the info.section */
1512 /* member must be adjusted by adding 3 */
1514 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1515 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1518 currfile
= mainfileindex
;
1522 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1523 /* oops file has changed... */
1524 for (i
= 0; i
< numfiles
; i
++)
1525 if (!strcmp(allfiles
[i
], ptr
->filename
))
1528 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1532 /* relocation table entry */
1533 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1534 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1537 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1540 /* relocation table entry */
1542 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1543 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1549 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1551 nasm_free(allfiles
);
1554 stablen
= (sptr
- sbuf
);
1555 stabrellen
= (rptr
- rbuf
);
1561 void stabs32_cleanup(void)
1563 struct linelist
*ptr
, *del
;
1575 nasm_free(stabrelbuf
);
1577 nasm_free(stabstrbuf
);