Add test from BR 2690688
[nasm.git] / output / outelf32.c
blob604c353a0cec1c89f779dbf0ca653678560ab0fe
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 license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "saa.h"
21 #include "raa.h"
22 #include "stdscan.h"
23 #include "outform.h"
24 #include "outlib.h"
25 #include "rbtree.h"
27 #ifdef OF_ELF32
30 * Relocation types.
32 enum reloc_type {
33 R_386_32 = 1, /* ordinary absolute relocation */
34 R_386_PC32 = 2, /* PC-relative relocation */
35 R_386_GOT32 = 3, /* an offset into GOT */
36 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
37 R_386_COPY = 5, /* ??? */
38 R_386_GLOB_DAT = 6, /* ??? */
39 R_386_JUMP_SLOT = 7, /* ??? */
40 R_386_RELATIVE = 8, /* ??? */
41 R_386_GOTOFF = 9, /* an offset from GOT base */
42 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
43 R_386_TLS_TPOFF = 14, /* Offset in static TLS block */
44 R_386_TLS_IE = 15, /* Address of GOT entry for static TLS
45 block offset */
46 /* These are GNU extensions, but useful */
47 R_386_16 = 20, /* A 16-bit absolute relocation */
48 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
49 R_386_8 = 22, /* An 8-bit absolute relocation */
50 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
53 struct Reloc {
54 struct Reloc *next;
55 int32_t address; /* relative to _start_ of section */
56 int32_t symbol; /* symbol index */
57 int type; /* type of relocation */
60 struct Symbol {
61 struct rbtree symv; /* symbol value and symbol rbtree */
62 int32_t strpos; /* string table position of name */
63 int32_t section; /* section ID of the symbol */
64 int type; /* symbol type */
65 int other; /* symbol visibility */
66 int32_t size; /* size of symbol */
67 int32_t globnum; /* symbol table offset if global */
68 struct Symbol *nextfwd; /* list of unresolved-size symbols */
69 char *name; /* used temporarily if in above list */
72 #define SHT_PROGBITS 1
73 #define SHT_NOBITS 8
75 #define SHF_WRITE 1
76 #define SHF_ALLOC 2
77 #define SHF_EXECINSTR 4
78 #define SHF_TLS (1 << 10) /* Section holds thread-local data. */
80 struct Section {
81 struct SAA *data;
82 uint32_t len, size, nrelocs;
83 int32_t index;
84 int type; /* SHT_PROGBITS or SHT_NOBITS */
85 int align; /* alignment: power of two */
86 uint32_t flags; /* section flags */
87 char *name;
88 struct SAA *rel;
89 int32_t rellen;
90 struct Reloc *head, **tail;
91 struct rbtree *gsyms; /* global symbols in section */
94 #define SECT_DELTA 32
95 static struct Section **sects;
96 static int nsects, sectlen;
98 #define SHSTR_DELTA 256
99 static char *shstrtab;
100 static int shstrtablen, shstrtabsize;
102 static struct SAA *syms;
103 static uint32_t nlocals, nglobs;
105 static int32_t def_seg;
107 static struct RAA *bsym;
109 static struct SAA *strs;
110 static uint32_t strslen;
112 static FILE *elffp;
113 static efunc error;
114 static evalfunc evaluate;
116 static struct Symbol *fwds;
118 static char elf_module[FILENAME_MAX];
120 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
121 static uint8_t elf_abiver = 0; /* Current ABI version */
123 extern struct ofmt of_elf32;
124 extern struct ofmt of_elf;
126 #define SHN_ABS 0xFFF1
127 #define SHN_COMMON 0xFFF2
128 #define SHN_UNDEF 0
130 #define SYM_GLOBAL 0x10
132 #define SHT_RELA 4 /* Relocation entries with addends */
134 #define STT_NOTYPE 0 /* Symbol type is unspecified */
135 #define STT_OBJECT 1 /* Symbol is a data object */
136 #define STT_FUNC 2 /* Symbol is a code object */
137 #define STT_SECTION 3 /* Symbol associated with a section */
138 #define STT_FILE 4 /* Symbol's name is file name */
139 #define STT_COMMON 5 /* Symbol is a common data object */
140 #define STT_TLS 6 /* Symbol is thread-local data object*/
141 #define STT_NUM 7 /* Number of defined types. */
143 #define STV_DEFAULT 0
144 #define STV_INTERNAL 1
145 #define STV_HIDDEN 2
146 #define STV_PROTECTED 3
148 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
150 #define SEG_ALIGN 16 /* alignment of sections in file */
151 #define SEG_ALIGN_1 (SEG_ALIGN-1)
153 /* Definitions in lieu of dwarf.h */
154 #define DW_TAG_compile_unit 0x11
155 #define DW_TAG_subprogram 0x2e
156 #define DW_AT_name 0x03
157 #define DW_AT_stmt_list 0x10
158 #define DW_AT_low_pc 0x11
159 #define DW_AT_high_pc 0x12
160 #define DW_AT_language 0x13
161 #define DW_AT_producer 0x25
162 #define DW_AT_frame_base 0x40
163 #define DW_FORM_addr 0x01
164 #define DW_FORM_data2 0x05
165 #define DW_FORM_data4 0x06
166 #define DW_FORM_string 0x08
167 #define DW_LNS_extended_op 0
168 #define DW_LNS_advance_pc 2
169 #define DW_LNS_advance_line 3
170 #define DW_LNS_set_file 4
171 #define DW_LNE_end_sequence 1
172 #define DW_LNE_set_address 2
173 #define DW_LNE_define_file 3
174 #define DW_LANG_Mips_Assembler 0x8001
176 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
178 static struct ELF_SECTDATA {
179 void *data;
180 int32_t len;
181 bool is_saa;
182 } *elf_sects;
183 static int elf_nsect, nsections;
184 static int32_t elf_foffs;
186 static void elf_write(void);
187 static void elf_sect_write(struct Section *, const uint8_t *,
188 uint32_t);
189 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
190 int, int);
191 static void elf_write_sections(void);
192 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
193 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
194 static void add_sectname(char *, char *);
196 /* this stuff is needed for the stabs debugging format */
197 #define N_SO 0x64 /* ID for main source file */
198 #define N_SOL 0x84 /* ID for sub-source file */
199 #define N_BINCL 0x82
200 #define N_EINCL 0xA2
201 #define N_SLINE 0x44
202 #define TY_STABSSYMLIN 0x40 /* ouch */
204 struct stabentry {
205 uint32_t n_strx;
206 uint8_t n_type;
207 uint8_t n_other;
208 uint16_t n_desc;
209 uint32_t n_value;
212 struct erel {
213 int offset, info;
216 struct symlininfo {
217 int offset;
218 int section; /* section index */
219 char *name; /* shallow-copied pointer of section name */
222 struct linelist {
223 struct symlininfo info;
224 int line;
225 char *filename;
226 struct linelist *next;
227 struct linelist *last;
230 struct sectlist {
231 struct SAA *psaa;
232 int section;
233 int line;
234 int offset;
235 int file;
236 struct sectlist *next;
237 struct sectlist *last;
240 /* common debug variables */
241 static int currentline = 1;
242 static int debug_immcall = 0;
244 /* stabs debug variables */
245 static struct linelist *stabslines = 0;
246 static int numlinestabs = 0;
247 static char *stabs_filename = 0;
248 static int symtabsection;
249 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
250 static int stablen, stabstrlen, stabrellen;
252 /* dwarf debug variables */
253 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
254 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
255 static int dwarf_numfiles = 0, dwarf_nsections;
256 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
257 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
258 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
259 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
260 abbrevlen, linelen, linerellen, framelen, loclen;
261 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
263 static struct dfmt df_dwarf;
264 static struct dfmt df_stabs;
265 static struct Symbol *lastsym;
267 /* common debugging routines */
268 void debug32_typevalue(int32_t);
269 void debug32_init(struct ofmt *, void *, FILE *, efunc);
270 void debug32_deflabel(char *, int32_t, int64_t, int, char *);
271 void debug32_directive(const char *, const char *);
273 /* stabs debugging routines */
274 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
275 void stabs32_output(int, void *);
276 void stabs32_generate(void);
277 void stabs32_cleanup(void);
279 /* dwarf debugging routines */
280 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
281 void dwarf32_output(int, void *);
282 void dwarf32_generate(void);
283 void dwarf32_cleanup(void);
284 void dwarf32_findfile(const char *);
285 void dwarf32_findsect(const int);
286 void saa_wleb128u(struct SAA *, int);
287 void saa_wleb128s(struct SAA *, int);
290 * Special section numbers which are used to define ELF special
291 * symbols, which can be used with WRT to provide PIC and TLS
292 * relocation types.
294 static int32_t elf_gotpc_sect, elf_gotoff_sect;
295 static int32_t elf_got_sect, elf_plt_sect;
296 static int32_t elf_sym_sect, elf_tlsie_sect;
298 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
300 elffp = fp;
301 error = errfunc;
302 evaluate = eval;
303 (void)ldef; /* placate optimisers */
304 sects = NULL;
305 nsects = sectlen = 0;
306 syms = saa_init((int32_t)sizeof(struct Symbol));
307 nlocals = nglobs = 0;
308 bsym = raa_init();
309 strs = saa_init(1L);
310 saa_wbytes(strs, "\0", 1L);
311 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
312 strslen = 2 + strlen(elf_module);
313 shstrtab = NULL;
314 shstrtablen = shstrtabsize = 0;;
315 add_sectname("", "");
317 fwds = NULL;
319 elf_gotpc_sect = seg_alloc();
320 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
321 error);
322 elf_gotoff_sect = seg_alloc();
323 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
324 error);
325 elf_got_sect = seg_alloc();
326 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
327 error);
328 elf_plt_sect = seg_alloc();
329 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
330 error);
331 elf_sym_sect = seg_alloc();
332 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
333 error);
334 elf_tlsie_sect = seg_alloc();
335 ldef("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false, &of_elf32,
336 error);
338 def_seg = seg_alloc();
341 static void elf_init_hack(FILE * fp, efunc errfunc, ldfunc ldef,
342 evalfunc eval)
344 of_elf32.current_dfmt = of_elf.current_dfmt; /* Sync debugging format */
345 elf_init(fp, errfunc, ldef, eval);
348 static void elf_cleanup(int debuginfo)
350 struct Reloc *r;
351 int i;
353 (void)debuginfo;
355 elf_write();
356 fclose(elffp);
357 for (i = 0; i < nsects; i++) {
358 if (sects[i]->type != SHT_NOBITS)
359 saa_free(sects[i]->data);
360 if (sects[i]->head)
361 saa_free(sects[i]->rel);
362 while (sects[i]->head) {
363 r = sects[i]->head;
364 sects[i]->head = sects[i]->head->next;
365 nasm_free(r);
368 nasm_free(sects);
369 saa_free(syms);
370 raa_free(bsym);
371 saa_free(strs);
372 if (of_elf32.current_dfmt) {
373 of_elf32.current_dfmt->cleanup();
377 static void add_sectname(char *firsthalf, char *secondhalf)
379 int len = strlen(firsthalf) + strlen(secondhalf);
380 while (shstrtablen + len + 1 > shstrtabsize)
381 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
382 strcpy(shstrtab + shstrtablen, firsthalf);
383 strcat(shstrtab + shstrtablen, secondhalf);
384 shstrtablen += len + 1;
387 static int elf_make_section(char *name, int type, int flags, int align)
389 struct Section *s;
391 s = nasm_malloc(sizeof(*s));
393 if (type != SHT_NOBITS)
394 s->data = saa_init(1L);
395 s->head = NULL;
396 s->tail = &s->head;
397 s->len = s->size = 0;
398 s->nrelocs = 0;
399 if (!strcmp(name, ".text"))
400 s->index = def_seg;
401 else
402 s->index = seg_alloc();
403 add_sectname("", name);
404 s->name = nasm_malloc(1 + strlen(name));
405 strcpy(s->name, name);
406 s->type = type;
407 s->flags = flags;
408 s->align = align;
409 s->gsyms = NULL;
411 if (nsects >= sectlen)
412 sects =
413 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
414 sects[nsects++] = s;
416 return nsects - 1;
419 static int32_t elf_section_names(char *name, int pass, int *bits)
421 char *p;
422 unsigned flags_and, flags_or;
423 int type, align, i;
426 * Default is 32 bits.
428 if (!name) {
429 *bits = 32;
430 return def_seg;
433 p = name;
434 while (*p && !nasm_isspace(*p))
435 p++;
436 if (*p)
437 *p++ = '\0';
438 flags_and = flags_or = type = align = 0;
440 while (*p && nasm_isspace(*p))
441 p++;
442 while (*p) {
443 char *q = p;
444 while (*p && !nasm_isspace(*p))
445 p++;
446 if (*p)
447 *p++ = '\0';
448 while (*p && nasm_isspace(*p))
449 p++;
451 if (!nasm_strnicmp(q, "align=", 6)) {
452 align = atoi(q + 6);
453 if (align == 0)
454 align = 1;
455 if ((align - 1) & align) { /* means it's not a power of two */
456 error(ERR_NONFATAL, "section alignment %d is not"
457 " a power of two", align);
458 align = 1;
460 } else if (!nasm_stricmp(q, "alloc")) {
461 flags_and |= SHF_ALLOC;
462 flags_or |= SHF_ALLOC;
463 } else if (!nasm_stricmp(q, "noalloc")) {
464 flags_and |= SHF_ALLOC;
465 flags_or &= ~SHF_ALLOC;
466 } else if (!nasm_stricmp(q, "exec")) {
467 flags_and |= SHF_EXECINSTR;
468 flags_or |= SHF_EXECINSTR;
469 } else if (!nasm_stricmp(q, "noexec")) {
470 flags_and |= SHF_EXECINSTR;
471 flags_or &= ~SHF_EXECINSTR;
472 } else if (!nasm_stricmp(q, "write")) {
473 flags_and |= SHF_WRITE;
474 flags_or |= SHF_WRITE;
475 } else if (!nasm_stricmp(q, "tls")) {
476 flags_and |= SHF_TLS;
477 flags_or |= SHF_TLS;
478 } else if (!nasm_stricmp(q, "nowrite")) {
479 flags_and |= SHF_WRITE;
480 flags_or &= ~SHF_WRITE;
481 } else if (!nasm_stricmp(q, "progbits")) {
482 type = SHT_PROGBITS;
483 } else if (!nasm_stricmp(q, "nobits")) {
484 type = SHT_NOBITS;
485 } else if (pass == 1) error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
486 " declaration of section `%s'", q, name);
489 if (!strcmp(name, ".comment") ||
490 !strcmp(name, ".shstrtab") ||
491 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
492 error(ERR_NONFATAL, "attempt to redefine reserved section"
493 "name `%s'", name);
494 return NO_SEG;
497 for (i = 0; i < nsects; i++)
498 if (!strcmp(name, sects[i]->name))
499 break;
500 if (i == nsects) {
501 if (!strcmp(name, ".text"))
502 i = elf_make_section(name, SHT_PROGBITS,
503 SHF_ALLOC | SHF_EXECINSTR, 16);
504 else if (!strcmp(name, ".rodata"))
505 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
506 else if (!strcmp(name, ".data"))
507 i = elf_make_section(name, SHT_PROGBITS,
508 SHF_ALLOC | SHF_WRITE, 4);
509 else if (!strcmp(name, ".bss"))
510 i = elf_make_section(name, SHT_NOBITS,
511 SHF_ALLOC | SHF_WRITE, 4);
512 else if (!strcmp(name, ".tdata"))
513 i = elf_make_section(name, SHT_PROGBITS,
514 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
515 else if (!strcmp(name, ".tbss"))
516 i = elf_make_section(name, SHT_NOBITS,
517 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
518 else
519 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
520 if (type)
521 sects[i]->type = type;
522 if (align)
523 sects[i]->align = align;
524 sects[i]->flags &= ~flags_and;
525 sects[i]->flags |= flags_or;
526 } else if (pass == 1) {
527 if ((type && sects[i]->type != type)
528 || (align && sects[i]->align != align)
529 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
530 error(ERR_WARNING, "section attributes ignored on"
531 " redeclaration of section `%s'", name);
534 return sects[i]->index;
537 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
538 int is_global, char *special)
540 int pos = strslen;
541 struct Symbol *sym;
542 bool special_used = false;
544 #if defined(DEBUG) && DEBUG>2
545 fprintf(stderr,
546 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
547 name, segment, offset, is_global, special);
548 #endif
549 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
551 * This is a NASM special symbol. We never allow it into
552 * the ELF symbol table, even if it's a valid one. If it
553 * _isn't_ a valid one, we should barf immediately.
555 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
556 strcmp(name, "..got") && strcmp(name, "..plt") &&
557 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
558 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
559 return;
562 if (is_global == 3) {
563 struct Symbol **s;
565 * Fix up a forward-reference symbol size from the first
566 * pass.
568 for (s = &fwds; *s; s = &(*s)->nextfwd)
569 if (!strcmp((*s)->name, name)) {
570 struct tokenval tokval;
571 expr *e;
572 char *p = special;
574 while (*p && !nasm_isspace(*p))
575 p++;
576 while (*p && nasm_isspace(*p))
577 p++;
578 stdscan_reset();
579 stdscan_bufptr = p;
580 tokval.t_type = TOKEN_INVALID;
581 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
582 if (e) {
583 if (!is_simple(e))
584 error(ERR_NONFATAL, "cannot use relocatable"
585 " expression as symbol size");
586 else
587 (*s)->size = reloc_value(e);
591 * Remove it from the list of unresolved sizes.
593 nasm_free((*s)->name);
594 *s = (*s)->nextfwd;
595 return;
597 return; /* it wasn't an important one */
600 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
601 strslen += 1 + strlen(name);
603 lastsym = sym = saa_wstruct(syms);
605 sym->strpos = pos;
606 sym->type = is_global ? SYM_GLOBAL : 0;
607 sym->other = STV_DEFAULT;
608 sym->size = 0;
609 if (segment == NO_SEG)
610 sym->section = SHN_ABS;
611 else {
612 int i;
613 sym->section = SHN_UNDEF;
614 if (nsects == 0 && segment == def_seg) {
615 int tempint;
616 if (segment != elf_section_names(".text", 2, &tempint))
617 error(ERR_PANIC,
618 "strange segment conditions in ELF driver");
619 sym->section = nsects;
620 } else {
621 for (i = 0; i < nsects; i++)
622 if (segment == sects[i]->index) {
623 sym->section = i + 1;
624 break;
629 if (is_global == 2) {
630 sym->size = offset;
631 sym->symv.key = 0;
632 sym->section = SHN_COMMON;
634 * We have a common variable. Check the special text to see
635 * if it's a valid number and power of two; if so, store it
636 * as the alignment for the common variable.
638 if (special) {
639 bool err;
640 sym->symv.key = readnum(special, &err);
641 if (err)
642 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
643 " valid number", special);
644 else if ((sym->symv.key | (sym->symv.key - 1))
645 != 2 * sym->symv.key - 1)
646 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
647 " power of two", special);
649 special_used = true;
650 } else
651 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
653 if (sym->type == SYM_GLOBAL) {
655 * If sym->section == SHN_ABS, then the first line of the
656 * else section would cause a core dump, because its a reference
657 * beyond the end of the section array.
658 * This behaviour is exhibited by this code:
659 * GLOBAL crash_nasm
660 * crash_nasm equ 0
661 * To avoid such a crash, such requests are silently discarded.
662 * This may not be the best solution.
664 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
665 bsym = raa_write(bsym, segment, nglobs);
666 } else if (sym->section != SHN_ABS) {
668 * This is a global symbol; so we must add it to the rbtree
669 * of global symbols in its section.
671 * In addition, we check the special text for symbol
672 * type and size information.
674 sects[sym->section-1]->gsyms =
675 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
677 if (special) {
678 int n = strcspn(special, " \t");
680 if (!nasm_strnicmp(special, "function", n))
681 sym->type |= STT_FUNC;
682 else if (!nasm_strnicmp(special, "data", n) ||
683 !nasm_strnicmp(special, "object", n))
684 sym->type |= STT_OBJECT;
685 else if (!nasm_strnicmp(special, "notype", n))
686 sym->type |= STT_NOTYPE;
687 else
688 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
689 n, special);
690 special += n;
692 while (nasm_isspace(*special))
693 ++special;
694 if (*special) {
695 n = strcspn(special, " \t");
696 if (!nasm_strnicmp(special, "default", n))
697 sym->other = STV_DEFAULT;
698 else if (!nasm_strnicmp(special, "internal", n))
699 sym->other = STV_INTERNAL;
700 else if (!nasm_strnicmp(special, "hidden", n))
701 sym->other = STV_HIDDEN;
702 else if (!nasm_strnicmp(special, "protected", n))
703 sym->other = STV_PROTECTED;
704 else
705 n = 0;
706 special += n;
709 if (*special) {
710 struct tokenval tokval;
711 expr *e;
712 int fwd = 0;
713 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
715 while (special[n] && nasm_isspace(special[n]))
716 n++;
718 * We have a size expression; attempt to
719 * evaluate it.
721 stdscan_reset();
722 stdscan_bufptr = special + n;
723 tokval.t_type = TOKEN_INVALID;
724 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
725 NULL);
726 if (fwd) {
727 sym->nextfwd = fwds;
728 fwds = sym;
729 sym->name = nasm_strdup(name);
730 } else if (e) {
731 if (!is_simple(e))
732 error(ERR_NONFATAL, "cannot use relocatable"
733 " expression as symbol size");
734 else
735 sym->size = reloc_value(e);
737 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
739 special_used = true;
742 * If TLS segment, mark symbol accordingly.
744 if (sects[sym->section - 1]->flags & SHF_TLS) {
745 sym->type &= 0xf0;
746 sym->type |= STT_TLS;
749 sym->globnum = nglobs;
750 nglobs++;
751 } else
752 nlocals++;
754 if (special && !special_used)
755 error(ERR_NONFATAL, "no special symbol features supported here");
758 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
760 struct Reloc *r;
762 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
763 sect->tail = &r->next;
764 r->next = NULL;
766 r->address = sect->len;
767 if (segment == NO_SEG)
768 r->symbol = 0;
769 else {
770 int i;
771 r->symbol = 0;
772 for (i = 0; i < nsects; i++)
773 if (segment == sects[i]->index)
774 r->symbol = i + 2;
775 if (!r->symbol)
776 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
778 r->type = type;
780 sect->nrelocs++;
784 * This routine deals with ..got and ..sym relocations: the more
785 * complicated kinds. In shared-library writing, some relocations
786 * with respect to global symbols must refer to the precise symbol
787 * rather than referring to an offset from the base of the section
788 * _containing_ the symbol. Such relocations call to this routine,
789 * which searches the symbol list for the symbol in question.
791 * R_386_GOT32 references require the _exact_ symbol address to be
792 * used; R_386_32 references can be at an offset from the symbol.
793 * The boolean argument `exact' tells us this.
795 * Return value is the adjusted value of `addr', having become an
796 * offset from the symbol rather than the section. Should always be
797 * zero when returning from an exact call.
799 * Limitation: if you define two symbols at the same place,
800 * confusion will occur.
802 * Inefficiency: we search, currently, using a linked list which
803 * isn't even necessarily sorted.
805 static int32_t elf_add_gsym_reloc(struct Section *sect,
806 int32_t segment, uint32_t offset,
807 int type, bool exact)
809 struct Reloc *r;
810 struct Section *s;
811 struct Symbol *sym;
812 struct rbtree *srb;
813 int i;
816 * First look up the segment/offset pair and find a global
817 * symbol corresponding to it. If it's not one of our segments,
818 * then it must be an external symbol, in which case we're fine
819 * doing a normal elf_add_reloc after first sanity-checking
820 * that the offset from the symbol is zero.
822 s = NULL;
823 for (i = 0; i < nsects; i++)
824 if (segment == sects[i]->index) {
825 s = sects[i];
826 break;
828 if (!s) {
829 if (exact && offset != 0)
830 error(ERR_NONFATAL, "unable to find a suitable global symbol"
831 " for this reference");
832 else
833 elf_add_reloc(sect, segment, type);
834 return offset;
837 srb = rb_search(s->gsyms, offset);
838 if (!srb || (exact && srb->key != offset)) {
839 error(ERR_NONFATAL, "unable to find a suitable global symbol"
840 " for this reference");
841 return 0;
843 sym = container_of(srb, struct Symbol, symv);
845 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
846 sect->tail = &r->next;
847 r->next = NULL;
849 r->address = sect->len;
850 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
851 r->type = type;
853 sect->nrelocs++;
855 return offset - sym->symv.key;
858 static void elf_out(int32_t segto, const void *data,
859 enum out_type type, uint64_t size,
860 int32_t segment, int32_t wrt)
862 struct Section *s;
863 int32_t addr;
864 uint8_t mydata[4], *p;
865 int i;
866 static struct symlininfo sinfo;
869 * handle absolute-assembly (structure definitions)
871 if (segto == NO_SEG) {
872 if (type != OUT_RESERVE)
873 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
874 " space");
875 return;
878 s = NULL;
879 for (i = 0; i < nsects; i++)
880 if (segto == sects[i]->index) {
881 s = sects[i];
882 break;
884 if (!s) {
885 int tempint; /* ignored */
886 if (segto != elf_section_names(".text", 2, &tempint))
887 error(ERR_PANIC, "strange segment conditions in ELF driver");
888 else {
889 s = sects[nsects - 1];
890 i = nsects - 1;
894 /* again some stabs debugging stuff */
895 if (of_elf32.current_dfmt) {
896 sinfo.offset = s->len;
897 sinfo.section = i;
898 sinfo.name = s->name;
899 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
901 /* end of debugging stuff */
903 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
904 error(ERR_WARNING, "attempt to initialize memory in"
905 " BSS section `%s': ignored", s->name);
906 s->len += realsize(type, size);
907 return;
910 if (type == OUT_RESERVE) {
911 if (s->type == SHT_PROGBITS) {
912 error(ERR_WARNING, "uninitialized space declared in"
913 " non-BSS section `%s': zeroing", s->name);
914 elf_sect_write(s, NULL, size);
915 } else
916 s->len += size;
917 } else if (type == OUT_RAWDATA) {
918 if (segment != NO_SEG)
919 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
920 elf_sect_write(s, data, size);
921 } else if (type == OUT_ADDRESS) {
922 bool gnu16 = false;
923 addr = *(int64_t *)data;
924 if (segment != NO_SEG) {
925 if (segment % 2) {
926 error(ERR_NONFATAL, "ELF format does not support"
927 " segment base references");
928 } else {
929 if (wrt == NO_SEG) {
930 if (size == 2) {
931 gnu16 = true;
932 elf_add_reloc(s, segment, R_386_16);
933 } else {
934 elf_add_reloc(s, segment, R_386_32);
936 } else if (wrt == elf_gotpc_sect + 1) {
938 * The user will supply GOT relative to $$. ELF
939 * will let us have GOT relative to $. So we
940 * need to fix up the data item by $-$$.
942 addr += s->len;
943 elf_add_reloc(s, segment, R_386_GOTPC);
944 } else if (wrt == elf_gotoff_sect + 1) {
945 elf_add_reloc(s, segment, R_386_GOTOFF);
946 } else if (wrt == elf_tlsie_sect + 1) {
947 addr = elf_add_gsym_reloc(s, segment, addr,
948 R_386_TLS_IE, true);
949 } else if (wrt == elf_got_sect + 1) {
950 addr = elf_add_gsym_reloc(s, segment, addr,
951 R_386_GOT32, true);
952 } else if (wrt == elf_sym_sect + 1) {
953 if (size == 2) {
954 gnu16 = true;
955 addr = elf_add_gsym_reloc(s, segment, addr,
956 R_386_16, false);
957 } else {
958 addr = elf_add_gsym_reloc(s, segment, addr,
959 R_386_32, false);
961 } else if (wrt == elf_plt_sect + 1) {
962 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
963 "relative PLT references");
964 } else {
965 error(ERR_NONFATAL, "ELF format does not support this"
966 " use of WRT");
967 wrt = NO_SEG; /* we can at least _try_ to continue */
971 p = mydata;
972 if (gnu16) {
973 error(ERR_WARNING | ERR_WARN_GNUELF,
974 "16-bit relocations in ELF is a GNU extension");
975 WRITESHORT(p, addr);
976 } else {
977 if (size != 4 && segment != NO_SEG) {
978 error(ERR_NONFATAL,
979 "Unsupported non-32-bit ELF relocation");
981 WRITELONG(p, addr);
983 elf_sect_write(s, mydata, size);
984 } else if (type == OUT_REL2ADR) {
985 if (segment == segto)
986 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
987 if (segment != NO_SEG && segment % 2) {
988 error(ERR_NONFATAL, "ELF format does not support"
989 " segment base references");
990 } else {
991 if (wrt == NO_SEG) {
992 error(ERR_WARNING | ERR_WARN_GNUELF,
993 "16-bit relocations in ELF is a GNU extension");
994 elf_add_reloc(s, segment, R_386_PC16);
995 } else {
996 error(ERR_NONFATAL,
997 "Unsupported non-32-bit ELF relocation");
1000 p = mydata;
1001 WRITESHORT(p, *(int64_t *)data - size);
1002 elf_sect_write(s, mydata, 2L);
1003 } else if (type == OUT_REL4ADR) {
1004 if (segment == segto)
1005 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1006 if (segment != NO_SEG && segment % 2) {
1007 error(ERR_NONFATAL, "ELF format does not support"
1008 " segment base references");
1009 } else {
1010 if (wrt == NO_SEG) {
1011 elf_add_reloc(s, segment, R_386_PC32);
1012 } else if (wrt == elf_plt_sect + 1) {
1013 elf_add_reloc(s, segment, R_386_PLT32);
1014 } else if (wrt == elf_gotpc_sect + 1 ||
1015 wrt == elf_gotoff_sect + 1 ||
1016 wrt == elf_got_sect + 1) {
1017 error(ERR_NONFATAL, "ELF format cannot produce PC-"
1018 "relative GOT references");
1019 } else {
1020 error(ERR_NONFATAL, "ELF format does not support this"
1021 " use of WRT");
1022 wrt = NO_SEG; /* we can at least _try_ to continue */
1025 p = mydata;
1026 WRITELONG(p, *(int64_t *)data - size);
1027 elf_sect_write(s, mydata, 4L);
1031 static void elf_write(void)
1033 int align;
1034 int scount;
1035 char *p;
1036 int commlen;
1037 char comment[64];
1038 int i;
1040 struct SAA *symtab;
1041 int32_t symtablen, symtablocal;
1044 * Work out how many sections we will have. We have SHN_UNDEF,
1045 * then the flexible user sections, then the four fixed
1046 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1047 * then optionally relocation sections for the user sections.
1049 if (of_elf32.current_dfmt == &df_stabs)
1050 nsections = 8;
1051 else if (of_elf32.current_dfmt == &df_dwarf)
1052 nsections = 15;
1053 else
1054 nsections = 5; /* SHN_UNDEF and the fixed ones */
1056 add_sectname("", ".comment");
1057 add_sectname("", ".shstrtab");
1058 add_sectname("", ".symtab");
1059 add_sectname("", ".strtab");
1060 for (i = 0; i < nsects; i++) {
1061 nsections++; /* for the section itself */
1062 if (sects[i]->head) {
1063 nsections++; /* for its relocations */
1064 add_sectname(".rel", sects[i]->name);
1068 if (of_elf32.current_dfmt == &df_stabs) {
1069 /* in case the debug information is wanted, just add these three sections... */
1070 add_sectname("", ".stab");
1071 add_sectname("", ".stabstr");
1072 add_sectname(".rel", ".stab");
1075 else if (of_elf32.current_dfmt == &df_dwarf) {
1076 /* the dwarf debug standard specifies the following ten sections,
1077 not all of which are currently implemented,
1078 although all of them are defined. */
1079 #define debug_aranges (int32_t) (nsections-10)
1080 #define debug_info (int32_t) (nsections-7)
1081 #define debug_abbrev (int32_t) (nsections-5)
1082 #define debug_line (int32_t) (nsections-4)
1083 add_sectname("", ".debug_aranges");
1084 add_sectname(".rela", ".debug_aranges");
1085 add_sectname("", ".debug_pubnames");
1086 add_sectname("", ".debug_info");
1087 add_sectname(".rela", ".debug_info");
1088 add_sectname("", ".debug_abbrev");
1089 add_sectname("", ".debug_line");
1090 add_sectname(".rela", ".debug_line");
1091 add_sectname("", ".debug_frame");
1092 add_sectname("", ".debug_loc");
1096 * Do the comment.
1098 *comment = '\0';
1099 commlen = 2 + snprintf(comment+1, sizeof comment-1, "%s", nasm_comment);
1102 * Output the ELF header.
1104 fwrite("\177ELF\1\1\1", 7, 1, elffp);
1105 fputc(elf_osabi, elffp);
1106 fputc(elf_abiver, elffp);
1107 fwritezero(7, elffp);
1108 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1109 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1110 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1111 fwriteint32_t(0L, elffp); /* no entry point */
1112 fwriteint32_t(0L, elffp); /* no program header table */
1113 fwriteint32_t(0x40L, elffp); /* section headers straight after
1114 * ELF header plus alignment */
1115 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1116 fwriteint16_t(0x34, elffp); /* size of ELF header */
1117 fwriteint16_t(0, elffp); /* no program header table, again */
1118 fwriteint16_t(0, elffp); /* still no program header table */
1119 fwriteint16_t(0x28, elffp); /* size of section header */
1120 fwriteint16_t(nsections, elffp); /* number of sections */
1121 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1122 * section header table */
1123 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1124 fwriteint32_t(0L, elffp);
1125 fwriteint32_t(0L, elffp);
1128 * Build the symbol table and relocation tables.
1130 symtab = elf_build_symtab(&symtablen, &symtablocal);
1131 for (i = 0; i < nsects; i++)
1132 if (sects[i]->head)
1133 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1134 sects[i]->head);
1137 * Now output the section header table.
1140 elf_foffs = 0x40 + 0x28 * nsections;
1141 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1142 elf_foffs += align;
1143 elf_nsect = 0;
1144 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1146 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1147 scount = 1; /* needed for the stabs debugging to track the symtable section */
1148 p = shstrtab + 1;
1149 for (i = 0; i < nsects; i++) {
1150 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1151 (sects[i]->type == SHT_PROGBITS ?
1152 sects[i]->data : NULL), true,
1153 sects[i]->len, 0, 0, sects[i]->align, 0);
1154 p += strlen(p) + 1;
1155 scount++; /* dito */
1157 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1158 scount++; /* dito */
1159 p += strlen(p) + 1;
1160 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1161 scount++; /* dito */
1162 p += strlen(p) + 1;
1163 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1164 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1165 p += strlen(p) + 1;
1166 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1167 for (i = 0; i < nsects; i++)
1168 if (sects[i]->head) {
1169 p += strlen(p) + 1;
1170 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
1171 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1173 if (of_elf32.current_dfmt == &df_stabs) {
1174 /* for debugging information, create the last three sections
1175 which are the .stab , .stabstr and .rel.stab sections respectively */
1177 /* this function call creates the stab sections in memory */
1178 stabs32_generate();
1180 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1181 p += strlen(p) + 1;
1182 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1183 nsections - 2, 0, 4, 12);
1185 p += strlen(p) + 1;
1186 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1187 stabstrlen, 0, 0, 4, 0);
1189 p += strlen(p) + 1;
1190 /* link -> symtable info -> section to refer to */
1191 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1192 stabrellen, symtabsection, nsections - 3, 4,
1196 else if (of_elf32.current_dfmt == &df_dwarf) {
1197 /* for dwarf debugging information, create the ten dwarf sections */
1199 /* this function call creates the dwarf sections in memory */
1200 if (dwarf_fsect) dwarf32_generate();
1202 p += strlen(p) + 1;
1203 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1204 arangeslen, 0, 0, 1, 0);
1205 p += strlen(p) + 1;
1206 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1207 arangesrellen, symtabsection, debug_aranges, 1, 12);
1208 p += strlen(p) + 1;
1209 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1210 pubnameslen, 0, 0, 1, 0);
1211 p += strlen(p) + 1;
1212 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1213 infolen, 0, 0, 1, 0);
1214 p += strlen(p) + 1;
1215 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1216 inforellen, symtabsection, debug_info, 1, 12);
1217 p += strlen(p) + 1;
1218 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1219 abbrevlen, 0, 0, 1, 0);
1220 p += strlen(p) + 1;
1221 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1222 linelen, 0, 0, 1, 0);
1223 p += strlen(p) + 1;
1224 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1225 linerellen, symtabsection, debug_line, 1, 12);
1226 p += strlen(p) + 1;
1227 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1228 framelen, 0, 0, 8, 0);
1229 p += strlen(p) + 1;
1230 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1231 loclen, 0, 0, 1, 0);
1234 fwritezero(align, elffp);
1237 * Now output the sections.
1239 elf_write_sections();
1241 nasm_free(elf_sects);
1242 saa_free(symtab);
1245 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1247 struct SAA *s = saa_init(1L);
1248 struct Symbol *sym;
1249 uint8_t entry[16], *p;
1250 int i;
1252 *len = *local = 0;
1255 * First, an all-zeros entry, required by the ELF spec.
1257 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1258 *len += 16;
1259 (*local)++;
1262 * Next, an entry for the file name.
1264 p = entry;
1265 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1266 WRITELONG(p, 0); /* no value */
1267 WRITELONG(p, 0); /* no size either */
1268 WRITESHORT(p, STT_FILE); /* type FILE */
1269 WRITESHORT(p, SHN_ABS);
1270 saa_wbytes(s, entry, 16L);
1271 *len += 16;
1272 (*local)++;
1275 * Now some standard symbols defining the segments, for relocation
1276 * purposes.
1278 for (i = 1; i <= nsects; i++) {
1279 p = entry;
1280 WRITELONG(p, 0); /* no symbol name */
1281 WRITELONG(p, 0); /* offset zero */
1282 WRITELONG(p, 0); /* size zero */
1283 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1284 WRITESHORT(p, i); /* section id */
1285 saa_wbytes(s, entry, 16L);
1286 *len += 16;
1287 (*local)++;
1291 * Now the other local symbols.
1293 saa_rewind(syms);
1294 while ((sym = saa_rstruct(syms))) {
1295 if (sym->type & SYM_GLOBAL)
1296 continue;
1297 p = entry;
1298 WRITELONG(p, sym->strpos);
1299 WRITELONG(p, sym->symv.key);
1300 WRITELONG(p, sym->size);
1301 WRITECHAR(p, sym->type); /* type and binding */
1302 WRITECHAR(p, sym->other); /* visibility */
1303 WRITESHORT(p, sym->section);
1304 saa_wbytes(s, entry, 16L);
1305 *len += 16;
1306 (*local)++;
1309 * dwarf needs symbols for debug sections
1310 * which are relocation targets.
1312 //*** fix for 32 bit
1313 if (of_elf32.current_dfmt == &df_dwarf) {
1314 dwarf_infosym = *local;
1315 p = entry;
1316 WRITELONG(p, 0); /* no symbol name */
1317 WRITELONG(p, (uint32_t) 0); /* offset zero */
1318 WRITELONG(p, (uint32_t) 0); /* size zero */
1319 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1320 WRITESHORT(p, debug_info); /* section id */
1321 saa_wbytes(s, entry, 16L);
1322 *len += 16;
1323 (*local)++;
1324 dwarf_abbrevsym = *local;
1325 p = entry;
1326 WRITELONG(p, 0); /* no symbol name */
1327 WRITELONG(p, (uint32_t) 0); /* offset zero */
1328 WRITELONG(p, (uint32_t) 0); /* size zero */
1329 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1330 WRITESHORT(p, debug_abbrev); /* section id */
1331 saa_wbytes(s, entry, 16L);
1332 *len += 16;
1333 (*local)++;
1334 dwarf_linesym = *local;
1335 p = entry;
1336 WRITELONG(p, 0); /* no symbol name */
1337 WRITELONG(p, (uint32_t) 0); /* offset zero */
1338 WRITELONG(p, (uint32_t) 0); /* size zero */
1339 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1340 WRITESHORT(p, debug_line); /* section id */
1341 saa_wbytes(s, entry, 16L);
1342 *len += 16;
1343 (*local)++;
1347 * Now the global symbols.
1349 saa_rewind(syms);
1350 while ((sym = saa_rstruct(syms))) {
1351 if (!(sym->type & SYM_GLOBAL))
1352 continue;
1353 p = entry;
1354 WRITELONG(p, sym->strpos);
1355 WRITELONG(p, sym->symv.key);
1356 WRITELONG(p, sym->size);
1357 WRITECHAR(p, sym->type); /* type and binding */
1358 WRITECHAR(p, sym->other); /* visibility */
1359 WRITESHORT(p, sym->section);
1360 saa_wbytes(s, entry, 16L);
1361 *len += 16;
1364 return s;
1367 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1369 struct SAA *s;
1370 uint8_t *p, entry[8];
1372 if (!r)
1373 return NULL;
1375 s = saa_init(1L);
1376 *len = 0;
1378 while (r) {
1379 int32_t sym = r->symbol;
1381 if (sym >= GLOBAL_TEMP_BASE)
1383 if (of_elf32.current_dfmt == &df_dwarf)
1384 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1385 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1388 p = entry;
1389 WRITELONG(p, r->address);
1390 WRITELONG(p, (sym << 8) + r->type);
1391 saa_wbytes(s, entry, 8L);
1392 *len += 8;
1394 r = r->next;
1397 return s;
1400 static void elf_section_header(int name, int type, int flags,
1401 void *data, bool is_saa, int32_t datalen,
1402 int link, int info, int align, int eltsize)
1404 elf_sects[elf_nsect].data = data;
1405 elf_sects[elf_nsect].len = datalen;
1406 elf_sects[elf_nsect].is_saa = is_saa;
1407 elf_nsect++;
1409 fwriteint32_t((int32_t)name, elffp);
1410 fwriteint32_t((int32_t)type, elffp);
1411 fwriteint32_t((int32_t)flags, elffp);
1412 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1413 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1414 fwriteint32_t(datalen, elffp);
1415 if (data)
1416 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1417 fwriteint32_t((int32_t)link, elffp);
1418 fwriteint32_t((int32_t)info, elffp);
1419 fwriteint32_t((int32_t)align, elffp);
1420 fwriteint32_t((int32_t)eltsize, elffp);
1423 static void elf_write_sections(void)
1425 int i;
1426 for (i = 0; i < elf_nsect; i++)
1427 if (elf_sects[i].data) {
1428 int32_t len = elf_sects[i].len;
1429 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1430 int32_t align = reallen - len;
1431 if (elf_sects[i].is_saa)
1432 saa_fpwrite(elf_sects[i].data, elffp);
1433 else
1434 fwrite(elf_sects[i].data, len, 1, elffp);
1435 fwritezero(align, elffp);
1439 static void elf_sect_write(struct Section *sect,
1440 const uint8_t *data, uint32_t len)
1442 saa_wbytes(sect->data, data, len);
1443 sect->len += len;
1446 static int32_t elf_segbase(int32_t segment)
1448 return segment;
1451 static int elf_directive(char *directive, char *value, int pass)
1453 bool err;
1454 int64_t n;
1455 char *p;
1457 if (!strcmp(directive, "osabi")) {
1458 if (pass == 2)
1459 return 1; /* ignore in pass 2 */
1461 n = readnum(value, &err);
1462 if (err) {
1463 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1464 return 1;
1466 if (n < 0 || n > 255) {
1467 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1468 return 1;
1470 elf_osabi = n;
1471 elf_abiver = 0;
1473 if ((p = strchr(value,',')) == NULL)
1474 return 1;
1476 n = readnum(p+1, &err);
1477 if (err || n < 0 || n > 255) {
1478 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1479 return 1;
1482 elf_abiver = n;
1483 return 1;
1486 return 0;
1489 static void elf_filename(char *inname, char *outname, efunc error)
1491 strcpy(elf_module, inname);
1492 standard_extension(inname, outname, ".o", error);
1495 extern macros_t elf_stdmac[];
1497 static int elf_set_info(enum geninfo type, char **val)
1499 (void)type;
1500 (void)val;
1501 return 0;
1503 static struct dfmt df_dwarf = {
1504 "ELF32 (i386) dwarf debug format for Linux",
1505 "dwarf",
1506 debug32_init,
1507 dwarf32_linenum,
1508 debug32_deflabel,
1509 debug32_directive,
1510 debug32_typevalue,
1511 dwarf32_output,
1512 dwarf32_cleanup
1514 static struct dfmt df_stabs = {
1515 "ELF32 (i386) stabs debug format for Linux",
1516 "stabs",
1517 debug32_init,
1518 stabs32_linenum,
1519 debug32_deflabel,
1520 debug32_directive,
1521 debug32_typevalue,
1522 stabs32_output,
1523 stabs32_cleanup
1526 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1528 struct ofmt of_elf32 = {
1529 "ELF32 (i386) object files (e.g. Linux)",
1530 "elf32",
1531 NULL,
1532 elf32_debugs_arr,
1533 &df_stabs,
1534 elf_stdmac,
1535 elf_init,
1536 elf_set_info,
1537 elf_out,
1538 elf_deflabel,
1539 elf_section_names,
1540 elf_segbase,
1541 elf_directive,
1542 elf_filename,
1543 elf_cleanup
1546 struct ofmt of_elf = {
1547 "ELF (short name for ELF32) ",
1548 "elf",
1549 NULL,
1550 elf32_debugs_arr,
1551 &df_stabs,
1552 elf_stdmac,
1553 elf_init_hack,
1554 elf_set_info,
1555 elf_out,
1556 elf_deflabel,
1557 elf_section_names,
1558 elf_segbase,
1559 elf_directive,
1560 elf_filename,
1561 elf_cleanup
1563 /* again, the stabs debugging stuff (code) */
1565 void debug32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1567 (void)of;
1568 (void)id;
1569 (void)fp;
1570 (void)error;
1573 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1575 (void)segto;
1577 if (!stabs_filename) {
1578 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1579 strcpy(stabs_filename, filename);
1580 } else {
1581 if (strcmp(stabs_filename, filename)) {
1582 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1583 in fact, this leak comes in quite handy to maintain a list of files
1584 encountered so far in the symbol lines... */
1586 /* why not nasm_free(stabs_filename); we're done with the old one */
1588 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1589 strcpy(stabs_filename, filename);
1592 debug_immcall = 1;
1593 currentline = linenumber;
1596 void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1597 char *special)
1599 (void)name;
1600 (void)segment;
1601 (void)offset;
1602 (void)is_global;
1603 (void)special;
1606 void debug32_directive(const char *directive, const char *params)
1608 (void)directive;
1609 (void)params;
1612 void debug32_typevalue(int32_t type)
1614 int32_t stype, ssize;
1615 switch (TYM_TYPE(type)) {
1616 case TY_LABEL:
1617 ssize = 0;
1618 stype = STT_NOTYPE;
1619 break;
1620 case TY_BYTE:
1621 ssize = 1;
1622 stype = STT_OBJECT;
1623 break;
1624 case TY_WORD:
1625 ssize = 2;
1626 stype = STT_OBJECT;
1627 break;
1628 case TY_DWORD:
1629 ssize = 4;
1630 stype = STT_OBJECT;
1631 break;
1632 case TY_FLOAT:
1633 ssize = 4;
1634 stype = STT_OBJECT;
1635 break;
1636 case TY_QWORD:
1637 ssize = 8;
1638 stype = STT_OBJECT;
1639 break;
1640 case TY_TBYTE:
1641 ssize = 10;
1642 stype = STT_OBJECT;
1643 break;
1644 case TY_OWORD:
1645 ssize = 8;
1646 stype = STT_OBJECT;
1647 break;
1648 case TY_COMMON:
1649 ssize = 0;
1650 stype = STT_COMMON;
1651 break;
1652 case TY_SEG:
1653 ssize = 0;
1654 stype = STT_SECTION;
1655 break;
1656 case TY_EXTERN:
1657 ssize = 0;
1658 stype = STT_NOTYPE;
1659 break;
1660 case TY_EQU:
1661 ssize = 0;
1662 stype = STT_NOTYPE;
1663 break;
1664 default:
1665 ssize = 0;
1666 stype = STT_NOTYPE;
1667 break;
1669 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1670 lastsym->size = ssize;
1671 lastsym->type = stype;
1675 void stabs32_output(int type, void *param)
1677 struct symlininfo *s;
1678 struct linelist *el;
1679 if (type == TY_STABSSYMLIN) {
1680 if (debug_immcall) {
1681 s = (struct symlininfo *)param;
1682 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1683 return; /* we are only interested in the text stuff */
1684 numlinestabs++;
1685 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1686 el->info.offset = s->offset;
1687 el->info.section = s->section;
1688 el->info.name = s->name;
1689 el->line = currentline;
1690 el->filename = stabs_filename;
1691 el->next = 0;
1692 if (stabslines) {
1693 stabslines->last->next = el;
1694 stabslines->last = el;
1695 } else {
1696 stabslines = el;
1697 stabslines->last = el;
1701 debug_immcall = 0;
1704 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1705 do {\
1706 WRITELONG(p,n_strx); \
1707 WRITECHAR(p,n_type); \
1708 WRITECHAR(p,n_other); \
1709 WRITESHORT(p,n_desc); \
1710 WRITELONG(p,n_value); \
1711 } while (0)
1713 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1715 void stabs32_generate(void)
1717 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1718 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1719 char **allfiles;
1720 int *fileidx;
1722 struct linelist *ptr;
1724 ptr = stabslines;
1726 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1727 for (i = 0; i < numlinestabs; i++)
1728 allfiles[i] = 0;
1729 numfiles = 0;
1730 while (ptr) {
1731 if (numfiles == 0) {
1732 allfiles[0] = ptr->filename;
1733 numfiles++;
1734 } else {
1735 for (i = 0; i < numfiles; i++) {
1736 if (!strcmp(allfiles[i], ptr->filename))
1737 break;
1739 if (i >= numfiles) {
1740 allfiles[i] = ptr->filename;
1741 numfiles++;
1744 ptr = ptr->next;
1746 strsize = 1;
1747 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1748 for (i = 0; i < numfiles; i++) {
1749 fileidx[i] = strsize;
1750 strsize += strlen(allfiles[i]) + 1;
1752 mainfileindex = 0;
1753 for (i = 0; i < numfiles; i++) {
1754 if (!strcmp(allfiles[i], elf_module)) {
1755 mainfileindex = i;
1756 break;
1760 /* worst case size of the stab buffer would be:
1761 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1763 sbuf =
1764 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1765 sizeof(struct stabentry));
1767 ssbuf = (uint8_t *)nasm_malloc(strsize);
1769 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1770 rptr = rbuf;
1772 for (i = 0; i < numfiles; i++) {
1773 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1775 ssbuf[0] = 0;
1777 stabstrlen = strsize; /* set global variable for length of stab strings */
1779 sptr = sbuf;
1780 ptr = stabslines;
1781 numstabs = 0;
1783 if (ptr) {
1784 /* this is the first stab, its strx points to the filename of the
1785 the source-file, the n_desc field should be set to the number
1786 of remaining stabs
1788 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1790 /* this is the stab for the main source file */
1791 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1793 /* relocation table entry */
1795 /* Since the symbol table has two entries before */
1796 /* the section symbols, the index in the info.section */
1797 /* member must be adjusted by adding 2 */
1799 WRITELONG(rptr, (sptr - sbuf) - 4);
1800 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1802 numstabs++;
1803 currfile = mainfileindex;
1806 while (ptr) {
1807 if (strcmp(allfiles[currfile], ptr->filename)) {
1808 /* oops file has changed... */
1809 for (i = 0; i < numfiles; i++)
1810 if (!strcmp(allfiles[i], ptr->filename))
1811 break;
1812 currfile = i;
1813 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1814 ptr->info.offset);
1815 numstabs++;
1817 /* relocation table entry */
1818 WRITELONG(rptr, (sptr - sbuf) - 4);
1819 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1822 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1823 numstabs++;
1825 /* relocation table entry */
1827 WRITELONG(rptr, (sptr - sbuf) - 4);
1828 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1830 ptr = ptr->next;
1834 ((struct stabentry *)sbuf)->n_desc = numstabs;
1836 nasm_free(allfiles);
1837 nasm_free(fileidx);
1839 stablen = (sptr - sbuf);
1840 stabrellen = (rptr - rbuf);
1841 stabrelbuf = rbuf;
1842 stabbuf = sbuf;
1843 stabstrbuf = ssbuf;
1846 void stabs32_cleanup(void)
1848 struct linelist *ptr, *del;
1849 if (!stabslines)
1850 return;
1851 ptr = stabslines;
1852 while (ptr) {
1853 del = ptr;
1854 ptr = ptr->next;
1855 nasm_free(del);
1857 if (stabbuf)
1858 nasm_free(stabbuf);
1859 if (stabrelbuf)
1860 nasm_free(stabrelbuf);
1861 if (stabstrbuf)
1862 nasm_free(stabstrbuf);
1864 /* dwarf routines */
1867 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1869 (void)segto;
1870 dwarf32_findfile(filename);
1871 debug_immcall = 1;
1872 currentline = linenumber;
1875 /* called from elf_out with type == TY_DEBUGSYMLIN */
1876 void dwarf32_output(int type, void *param)
1878 int ln, aa, inx, maxln, soc;
1879 struct symlininfo *s;
1880 struct SAA *plinep;
1882 (void)type;
1884 s = (struct symlininfo *)param;
1885 /* line number info is only gathered for executable sections */
1886 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1887 return;
1888 /* Check if section index has changed */
1889 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1891 dwarf32_findsect(s->section);
1893 /* do nothing unless line or file has changed */
1894 if (debug_immcall)
1896 ln = currentline - dwarf_csect->line;
1897 aa = s->offset - dwarf_csect->offset;
1898 inx = dwarf_clist->line;
1899 plinep = dwarf_csect->psaa;
1900 /* check for file change */
1901 if (!(inx == dwarf_csect->file))
1903 saa_write8(plinep,DW_LNS_set_file);
1904 saa_write8(plinep,inx);
1905 dwarf_csect->file = inx;
1907 /* check for line change */
1908 if (ln)
1910 /* test if in range of special op code */
1911 maxln = line_base + line_range;
1912 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1913 if (ln >= line_base && ln < maxln && soc < 256)
1915 saa_write8(plinep,soc);
1917 else
1919 if (ln)
1921 saa_write8(plinep,DW_LNS_advance_line);
1922 saa_wleb128s(plinep,ln);
1924 if (aa)
1926 saa_write8(plinep,DW_LNS_advance_pc);
1927 saa_wleb128u(plinep,aa);
1930 dwarf_csect->line = currentline;
1931 dwarf_csect->offset = s->offset;
1933 /* show change handled */
1934 debug_immcall = 0;
1939 void dwarf32_generate(void)
1941 uint8_t *pbuf;
1942 int indx;
1943 struct linelist *ftentry;
1944 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1945 struct SAA *parangesrel, *plinesrel, *pinforel;
1946 struct sectlist *psect;
1947 size_t saalen, linepoff, totlen, highaddr;
1949 /* write epilogues for each line program range */
1950 /* and build aranges section */
1951 paranges = saa_init(1L);
1952 parangesrel = saa_init(1L);
1953 saa_write16(paranges,2); /* dwarf version */
1954 saa_write32(parangesrel, paranges->datalen+4);
1955 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1956 saa_write32(parangesrel, 0);
1957 saa_write32(paranges,0); /* offset into info */
1958 saa_write8(paranges,4); /* pointer size */
1959 saa_write8(paranges,0); /* not segmented */
1960 saa_write32(paranges,0); /* padding */
1961 /* iterate though sectlist entries */
1962 psect = dwarf_fsect;
1963 totlen = 0;
1964 highaddr = 0;
1965 for (indx = 0; indx < dwarf_nsections; indx++)
1967 plinep = psect->psaa;
1968 /* Line Number Program Epilogue */
1969 saa_write8(plinep,2); /* std op 2 */
1970 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1971 saa_write8(plinep,DW_LNS_extended_op);
1972 saa_write8(plinep,1); /* operand length */
1973 saa_write8(plinep,DW_LNE_end_sequence);
1974 totlen += plinep->datalen;
1975 /* range table relocation entry */
1976 saa_write32(parangesrel, paranges->datalen + 4);
1977 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1978 saa_write32(parangesrel, (uint32_t) 0);
1979 /* range table entry */
1980 saa_write32(paranges,0x0000); /* range start */
1981 saa_write32(paranges,sects[psect->section]->len); /* range length */
1982 highaddr += sects[psect->section]->len;
1983 /* done with this entry */
1984 psect = psect->next;
1986 saa_write32(paranges,0); /* null address */
1987 saa_write32(paranges,0); /* null length */
1988 saalen = paranges->datalen;
1989 arangeslen = saalen + 4;
1990 arangesbuf = pbuf = nasm_malloc(arangeslen);
1991 WRITELONG(pbuf,saalen); /* initial length */
1992 saa_rnbytes(paranges, pbuf, saalen);
1993 saa_free(paranges);
1995 /* build rela.aranges section */
1996 arangesrellen = saalen = parangesrel->datalen;
1997 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1998 saa_rnbytes(parangesrel, pbuf, saalen);
1999 saa_free(parangesrel);
2001 /* build pubnames section */
2002 ppubnames = saa_init(1L);
2003 saa_write16(ppubnames,3); /* dwarf version */
2004 saa_write32(ppubnames,0); /* offset into info */
2005 saa_write32(ppubnames,0); /* space used in info */
2006 saa_write32(ppubnames,0); /* end of list */
2007 saalen = ppubnames->datalen;
2008 pubnameslen = saalen + 4;
2009 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2010 WRITELONG(pbuf,saalen); /* initial length */
2011 saa_rnbytes(ppubnames, pbuf, saalen);
2012 saa_free(ppubnames);
2014 /* build info section */
2015 pinfo = saa_init(1L);
2016 pinforel = saa_init(1L);
2017 saa_write16(pinfo,2); /* dwarf version */
2018 saa_write32(pinforel, pinfo->datalen + 4);
2019 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
2020 saa_write32(pinforel, 0);
2021 saa_write32(pinfo,0); /* offset into abbrev */
2022 saa_write8(pinfo,4); /* pointer size */
2023 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2024 saa_write32(pinforel, pinfo->datalen + 4);
2025 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2026 saa_write32(pinforel, 0);
2027 saa_write32(pinfo,0); /* DW_AT_low_pc */
2028 saa_write32(pinforel, pinfo->datalen + 4);
2029 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2030 saa_write32(pinforel, 0);
2031 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
2032 saa_write32(pinforel, pinfo->datalen + 4);
2033 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
2034 saa_write32(pinforel, 0);
2035 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2036 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2037 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2038 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2039 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2040 saa_write32(pinforel, pinfo->datalen + 4);
2041 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2042 saa_write32(pinforel, 0);
2043 saa_write32(pinfo,0); /* DW_AT_low_pc */
2044 saa_write32(pinfo,0); /* DW_AT_frame_base */
2045 saa_write8(pinfo,0); /* end of entries */
2046 saalen = pinfo->datalen;
2047 infolen = saalen + 4;
2048 infobuf = pbuf = nasm_malloc(infolen);
2049 WRITELONG(pbuf,saalen); /* initial length */
2050 saa_rnbytes(pinfo, pbuf, saalen);
2051 saa_free(pinfo);
2053 /* build rela.info section */
2054 inforellen = saalen = pinforel->datalen;
2055 inforelbuf = pbuf = nasm_malloc(inforellen);
2056 saa_rnbytes(pinforel, pbuf, saalen);
2057 saa_free(pinforel);
2059 /* build abbrev section */
2060 pabbrev = saa_init(1L);
2061 saa_write8(pabbrev,1); /* entry number LEB128u */
2062 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2063 saa_write8(pabbrev,1); /* has children */
2064 /* the following attributes and forms are all LEB128u values */
2065 saa_write8(pabbrev,DW_AT_low_pc);
2066 saa_write8(pabbrev,DW_FORM_addr);
2067 saa_write8(pabbrev,DW_AT_high_pc);
2068 saa_write8(pabbrev,DW_FORM_addr);
2069 saa_write8(pabbrev,DW_AT_stmt_list);
2070 saa_write8(pabbrev,DW_FORM_data4);
2071 saa_write8(pabbrev,DW_AT_name);
2072 saa_write8(pabbrev,DW_FORM_string);
2073 saa_write8(pabbrev,DW_AT_producer);
2074 saa_write8(pabbrev,DW_FORM_string);
2075 saa_write8(pabbrev,DW_AT_language);
2076 saa_write8(pabbrev,DW_FORM_data2);
2077 saa_write16(pabbrev,0); /* end of entry */
2078 /* LEB128u usage same as above */
2079 saa_write8(pabbrev,2); /* entry number */
2080 saa_write8(pabbrev,DW_TAG_subprogram);
2081 saa_write8(pabbrev,0); /* no children */
2082 saa_write8(pabbrev,DW_AT_low_pc);
2083 saa_write8(pabbrev,DW_FORM_addr);
2084 saa_write8(pabbrev,DW_AT_frame_base);
2085 saa_write8(pabbrev,DW_FORM_data4);
2086 saa_write16(pabbrev,0); /* end of entry */
2087 abbrevlen = saalen = pabbrev->datalen;
2088 abbrevbuf = pbuf = nasm_malloc(saalen);
2089 saa_rnbytes(pabbrev, pbuf, saalen);
2090 saa_free(pabbrev);
2092 /* build line section */
2093 /* prolog */
2094 plines = saa_init(1L);
2095 saa_write8(plines,1); /* Minimum Instruction Length */
2096 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2097 saa_write8(plines,line_base); /* Line Base */
2098 saa_write8(plines,line_range); /* Line Range */
2099 saa_write8(plines,opcode_base); /* Opcode Base */
2100 /* standard opcode lengths (# of LEB128u operands) */
2101 saa_write8(plines,0); /* Std opcode 1 length */
2102 saa_write8(plines,1); /* Std opcode 2 length */
2103 saa_write8(plines,1); /* Std opcode 3 length */
2104 saa_write8(plines,1); /* Std opcode 4 length */
2105 saa_write8(plines,1); /* Std opcode 5 length */
2106 saa_write8(plines,0); /* Std opcode 6 length */
2107 saa_write8(plines,0); /* Std opcode 7 length */
2108 saa_write8(plines,0); /* Std opcode 8 length */
2109 saa_write8(plines,1); /* Std opcode 9 length */
2110 saa_write8(plines,0); /* Std opcode 10 length */
2111 saa_write8(plines,0); /* Std opcode 11 length */
2112 saa_write8(plines,1); /* Std opcode 12 length */
2113 /* Directory Table */
2114 saa_write8(plines,0); /* End of table */
2115 /* File Name Table */
2116 ftentry = dwarf_flist;
2117 for (indx = 0;indx<dwarf_numfiles;indx++)
2119 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2120 saa_write8(plines,0); /* directory LEB128u */
2121 saa_write8(plines,0); /* time LEB128u */
2122 saa_write8(plines,0); /* size LEB128u */
2123 ftentry = ftentry->next;
2125 saa_write8(plines,0); /* End of table */
2126 linepoff = plines->datalen;
2127 linelen = linepoff + totlen + 10;
2128 linebuf = pbuf = nasm_malloc(linelen);
2129 WRITELONG(pbuf,linelen-4); /* initial length */
2130 WRITESHORT(pbuf,3); /* dwarf version */
2131 WRITELONG(pbuf,linepoff); /* offset to line number program */
2132 /* write line header */
2133 saalen = linepoff;
2134 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2135 pbuf += linepoff;
2136 saa_free(plines);
2137 /* concatonate line program ranges */
2138 linepoff += 13;
2139 plinesrel = saa_init(1L);
2140 psect = dwarf_fsect;
2141 for (indx = 0; indx < dwarf_nsections; indx++)
2143 saa_write32(plinesrel, linepoff);
2144 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2145 saa_write32(plinesrel, (uint32_t) 0);
2146 plinep = psect->psaa;
2147 saalen = plinep->datalen;
2148 saa_rnbytes(plinep, pbuf, saalen);
2149 pbuf += saalen;
2150 linepoff += saalen;
2151 saa_free(plinep);
2152 /* done with this entry */
2153 psect = psect->next;
2157 /* build rela.lines section */
2158 linerellen =saalen = plinesrel->datalen;
2159 linerelbuf = pbuf = nasm_malloc(linerellen);
2160 saa_rnbytes(plinesrel, pbuf, saalen);
2161 saa_free(plinesrel);
2163 /* build frame section */
2164 framelen = 4;
2165 framebuf = pbuf = nasm_malloc(framelen);
2166 WRITELONG(pbuf,framelen-4); /* initial length */
2168 /* build loc section */
2169 loclen = 16;
2170 locbuf = pbuf = nasm_malloc(loclen);
2171 WRITELONG(pbuf,0); /* null beginning offset */
2172 WRITELONG(pbuf,0); /* null ending offset */
2175 void dwarf32_cleanup(void)
2177 if (arangesbuf)
2178 nasm_free(arangesbuf);
2179 if (arangesrelbuf)
2180 nasm_free(arangesrelbuf);
2181 if (pubnamesbuf)
2182 nasm_free(pubnamesbuf);
2183 if (infobuf)
2184 nasm_free(infobuf);
2185 if (inforelbuf)
2186 nasm_free(inforelbuf);
2187 if (abbrevbuf)
2188 nasm_free(abbrevbuf);
2189 if (linebuf)
2190 nasm_free(linebuf);
2191 if (linerelbuf)
2192 nasm_free(linerelbuf);
2193 if (framebuf)
2194 nasm_free(framebuf);
2195 if (locbuf)
2196 nasm_free(locbuf);
2198 void dwarf32_findfile(const char * fname)
2200 int finx;
2201 struct linelist *match;
2203 /* return if fname is current file name */
2204 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2205 /* search for match */
2206 else
2208 match = 0;
2209 if (dwarf_flist)
2211 match = dwarf_flist;
2212 for (finx = 0; finx < dwarf_numfiles; finx++)
2214 if (!(strcmp(fname, match->filename)))
2216 dwarf_clist = match;
2217 return;
2221 /* add file name to end of list */
2222 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2223 dwarf_numfiles++;
2224 dwarf_clist->line = dwarf_numfiles;
2225 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2226 strcpy(dwarf_clist->filename,fname);
2227 dwarf_clist->next = 0;
2228 /* if first entry */
2229 if (!dwarf_flist)
2231 dwarf_flist = dwarf_elist = dwarf_clist;
2232 dwarf_clist->last = 0;
2234 /* chain to previous entry */
2235 else
2237 dwarf_elist->next = dwarf_clist;
2238 dwarf_elist = dwarf_clist;
2242 /* */
2243 void dwarf32_findsect(const int index)
2245 int sinx;
2246 struct sectlist *match;
2247 struct SAA *plinep;
2248 /* return if index is current section index */
2249 if (dwarf_csect && (dwarf_csect->section == index))
2251 return;
2253 /* search for match */
2254 else
2256 match = 0;
2257 if (dwarf_fsect)
2259 match = dwarf_fsect;
2260 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2262 if ((match->section == index))
2264 dwarf_csect = match;
2265 return;
2267 match = match->next;
2270 /* add entry to end of list */
2271 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2272 dwarf_nsections++;
2273 dwarf_csect->psaa = plinep = saa_init(1L);
2274 dwarf_csect->line = 1;
2275 dwarf_csect->offset = 0;
2276 dwarf_csect->file = 1;
2277 dwarf_csect->section = index;
2278 dwarf_csect->next = 0;
2279 /* set relocatable address at start of line program */
2280 saa_write8(plinep,DW_LNS_extended_op);
2281 saa_write8(plinep,5); /* operand length */
2282 saa_write8(plinep,DW_LNE_set_address);
2283 saa_write32(plinep,0); /* Start Address */
2284 /* if first entry */
2285 if (!dwarf_fsect)
2287 dwarf_fsect = dwarf_esect = dwarf_csect;
2288 dwarf_csect->last = 0;
2290 /* chain to previous entry */
2291 else
2293 dwarf_esect->next = dwarf_csect;
2294 dwarf_esect = dwarf_csect;
2299 #endif /* OF_ELF */