NASM 0.99.05
[nasm/avx512.git] / output / outelf64.c
bloba72a6bf4c10817ddfecf2a2743038bd5d23f2c6a
1 /* outelf.c output routines for the Netwide Assembler to produce
2 * ELF64 (x86_64 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.
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 "stdscan.h"
21 #include "outform.h"
23 /* Definitions in lieu of elf.h */
25 #define SHT_PROGBITS 1
26 #define SHT_RELA 4 /* Relocation entries with addends */
27 #define SHT_NOBITS 8
28 #define SHF_WRITE (1 << 0) /* Writable */
29 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
30 #define SHF_EXECINSTR (1 << 2) /* Executable */
31 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
32 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
33 #define R_X86_64_NONE 0 /* No reloc */
34 #define R_X86_64_64 1 /* Direct 64 bit address */
35 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
36 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
37 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
38 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
39 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
40 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
41 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
42 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
43 #define ET_REL 1 /* Relocatable file */
44 #define EM_X86_64 62 /* AMD x86-64 architecture */
45 typedef uint32_t Elf64_Word;
46 typedef uint64_t Elf64_Xword;
47 typedef uint64_t Elf64_Addr;
48 typedef uint64_t Elf64_Off;
49 typedef struct
51 Elf64_Word sh_name; /* Section name (string tbl index) */
52 Elf64_Word sh_type; /* Section type */
53 Elf64_Xword sh_flags; /* Section flags */
54 Elf64_Addr sh_addr; /* Section virtual addr at execution */
55 Elf64_Off sh_offset; /* Section file offset */
56 Elf64_Xword sh_size; /* Section size in bytes */
57 Elf64_Word sh_link; /* Link to another section */
58 Elf64_Word sh_info; /* Additional section information */
59 Elf64_Xword sh_addralign; /* Section alignment */
60 Elf64_Xword sh_entsize; /* Entry size if section holds table */
61 } Elf64_Shdr;
64 #ifdef OF_ELF64
67 struct Reloc {
68 struct Reloc *next;
69 int64_t address; /* relative to _start_ of section */
70 int64_t symbol; /* ELF symbol info thingy */
71 int type; /* type of relocation */
74 struct Symbol {
75 int32_t strpos; /* string table position of name */
76 int32_t section; /* section ID of the symbol */
77 int type; /* symbol type */
78 int other; /* symbol visibility */
79 int64_t value; /* address, or COMMON variable align */
80 int32_t size; /* size of symbol */
81 int32_t globnum; /* symbol table offset if global */
82 struct Symbol *next; /* list of globals in each section */
83 struct Symbol *nextfwd; /* list of unresolved-size symbols */
84 char *name; /* used temporarily if in above list */
88 struct Section {
89 struct SAA *data;
90 uint32_t len, size, nrelocs;
91 int32_t index;
92 int type; /* SHT_PROGBITS or SHT_NOBITS */
93 int align; /* alignment: power of two */
94 uint32_t flags; /* section flags */
95 char *name;
96 struct SAA *rel;
97 int32_t rellen;
98 struct Reloc *head, **tail;
99 struct Symbol *gsyms; /* global symbols in section */
102 #define SECT_DELTA 32
103 static struct Section **sects;
104 static int nsects, sectlen;
106 #define SHSTR_DELTA 256
107 static char *shstrtab;
108 static int shstrtablen, shstrtabsize;
110 static struct SAA *syms;
111 static uint32_t nlocals, nglobs;
113 static int32_t def_seg;
115 static struct RAA *bsym;
117 static struct SAA *strs;
118 static uint32_t strslen;
120 static FILE *elffp;
121 static efunc error;
122 static evalfunc evaluate;
124 static struct Symbol *fwds;
126 static char elf_module[FILENAME_MAX];
128 extern struct ofmt of_elf64;
130 #define SHN_UNDEF 0
132 #define SYM_SECTION 0x04
133 #define SYM_GLOBAL 0x10
134 #define SYM_NOTYPE 0x00
135 #define SYM_DATA 0x01
136 #define SYM_FUNCTION 0x02
138 #define STV_DEFAULT 0
139 #define STV_INTERNAL 1
140 #define STV_HIDDEN 2
141 #define STV_PROTECTED 3
143 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
145 #define SEG_ALIGN 16 /* alignment of sections in file */
146 #define SEG_ALIGN_1 (SEG_ALIGN-1)
148 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
150 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
151 static struct ELF_SECTDATA {
152 void *data;
153 int32_t len;
154 bool is_saa;
155 } *elf_sects;
156 static int elf_nsect;
157 static int32_t elf_foffs;
159 static void elf_write(void);
160 static void elf_sect_write(struct Section *, const uint8_t *,
161 uint32_t);
162 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
163 int, int);
164 static void elf_write_sections(void);
165 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
166 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
167 static void add_sectname(char *, char *);
169 /* this stuff is needed for the stabs debugging format */
170 #define N_SO 0x64 /* ID for main source file */
171 #define N_SOL 0x84 /* ID for sub-source file */
172 #define N_BINCL 0x82
173 #define N_EINCL 0xA2
174 #define N_SLINE 0x44
175 #define TY_STABSSYMLIN 0x40 /* ouch */
177 struct stabentry {
178 uint32_t n_strx;
179 uint8_t n_type;
180 uint8_t n_other;
181 uint16_t n_desc;
182 uint32_t n_value;
185 struct erel {
186 int offset, info;
189 struct symlininfo {
190 int offset;
191 int section; /* section index */
192 char *name; /* shallow-copied pointer of section name */
195 struct linelist {
196 struct symlininfo info;
197 int line;
198 char *filename;
199 struct linelist *next;
200 struct linelist *last;
203 static struct linelist *stabslines = 0;
204 static int stabs_immcall = 0;
205 static int currentline = 0;
206 static int numlinestabs = 0;
207 static char *stabs_filename = 0;
208 static int symtabsection;
209 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
210 static int stablen, stabstrlen, stabrellen;
212 static struct dfmt df_stabs;
214 void stabs64_init(struct ofmt *, void *, FILE *, efunc);
215 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
216 void stabs64_deflabel(char *, int32_t, int32_t, int, char *);
217 void stabs64_directive(const char *, const char *);
218 void stabs64_typevalue(int32_t);
219 void stabs64_output(int, void *);
220 void stabs64_generate(void);
221 void stabs64_cleanup(void);
223 /* end of stabs debugging stuff */
226 * Special section numbers which are used to define ELF special
227 * symbols, which can be used with WRT to provide PIC relocation
228 * types.
230 static int32_t elf_gotpc_sect, elf_gotoff_sect;
231 static int32_t elf_got_sect, elf_plt_sect;
232 static int32_t elf_sym_sect;
234 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
236 maxbits = 64;
237 elffp = fp;
238 error = errfunc;
239 evaluate = eval;
240 (void)ldef; /* placate optimisers */
241 sects = NULL;
242 nsects = sectlen = 0;
243 syms = saa_init((int32_t)sizeof(struct Symbol));
244 nlocals = nglobs = 0;
245 bsym = raa_init();
246 strs = saa_init(1L);
247 saa_wbytes(strs, "\0", 1L);
248 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
249 strslen = 2 + strlen(elf_module);
250 shstrtab = NULL;
251 shstrtablen = shstrtabsize = 0;;
252 add_sectname("", "");
254 fwds = NULL;
256 elf_gotpc_sect = seg_alloc();
257 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
258 error);
259 elf_gotoff_sect = seg_alloc();
260 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
261 error);
262 elf_got_sect = seg_alloc();
263 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
264 error);
265 elf_plt_sect = seg_alloc();
266 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
267 error);
268 elf_sym_sect = seg_alloc();
269 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
270 error);
272 def_seg = seg_alloc();
275 static void elf_cleanup(int debuginfo)
277 struct Reloc *r;
278 int i;
280 (void)debuginfo;
282 elf_write();
283 fclose(elffp);
284 for (i = 0; i < nsects; i++) {
285 if (sects[i]->type != SHT_NOBITS)
286 saa_free(sects[i]->data);
287 if (sects[i]->head)
288 saa_free(sects[i]->rel);
289 while (sects[i]->head) {
290 r = sects[i]->head;
291 sects[i]->head = sects[i]->head->next;
292 nasm_free(r);
295 nasm_free(sects);
296 saa_free(syms);
297 raa_free(bsym);
298 saa_free(strs);
299 if (of_elf64.current_dfmt) {
300 of_elf64.current_dfmt->cleanup();
304 static void add_sectname(char *firsthalf, char *secondhalf)
306 int len = strlen(firsthalf) + strlen(secondhalf);
307 while (shstrtablen + len + 1 > shstrtabsize)
308 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
309 strcpy(shstrtab + shstrtablen, firsthalf);
310 strcat(shstrtab + shstrtablen, secondhalf);
311 shstrtablen += len + 1;
314 static int elf_make_section(char *name, int type, int flags, int align)
316 struct Section *s;
318 s = nasm_malloc(sizeof(*s));
320 if (type != SHT_NOBITS)
321 s->data = saa_init(1L);
322 s->head = NULL;
323 s->tail = &s->head;
324 s->len = s->size = 0;
325 s->nrelocs = 0;
326 if (!strcmp(name, ".text"))
327 s->index = def_seg;
328 else
329 s->index = seg_alloc();
330 add_sectname("", name);
331 s->name = nasm_malloc(1 + strlen(name));
332 strcpy(s->name, name);
333 s->type = type;
334 s->flags = flags;
335 s->align = align;
336 s->gsyms = NULL;
338 if (nsects >= sectlen)
339 sects =
340 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
341 sects[nsects++] = s;
343 return nsects - 1;
346 static int32_t elf_section_names(char *name, int pass, int *bits)
348 char *p;
349 int flags_and, flags_or, type, align, i;
352 * Default is 64 bits.
354 if (!name) {
355 *bits = 64;
356 return def_seg;
359 p = name;
360 while (*p && !isspace(*p))
361 p++;
362 if (*p)
363 *p++ = '\0';
364 flags_and = flags_or = type = align = 0;
366 while (*p && isspace(*p))
367 p++;
368 while (*p) {
369 char *q = p;
370 while (*p && !isspace(*p))
371 p++;
372 if (*p)
373 *p++ = '\0';
374 while (*p && isspace(*p))
375 p++;
377 if (!nasm_strnicmp(q, "align=", 6)) {
378 align = atoi(q + 6);
379 if (align == 0)
380 align = 1;
381 if ((align - 1) & align) { /* means it's not a power of two */
382 error(ERR_NONFATAL, "section alignment %d is not"
383 " a power of two", align);
384 align = 1;
386 } else if (!nasm_stricmp(q, "alloc")) {
387 flags_and |= SHF_ALLOC;
388 flags_or |= SHF_ALLOC;
389 } else if (!nasm_stricmp(q, "noalloc")) {
390 flags_and |= SHF_ALLOC;
391 flags_or &= ~SHF_ALLOC;
392 } else if (!nasm_stricmp(q, "exec")) {
393 flags_and |= SHF_EXECINSTR;
394 flags_or |= SHF_EXECINSTR;
395 } else if (!nasm_stricmp(q, "noexec")) {
396 flags_and |= SHF_EXECINSTR;
397 flags_or &= ~SHF_EXECINSTR;
398 } else if (!nasm_stricmp(q, "write")) {
399 flags_and |= SHF_WRITE;
400 flags_or |= SHF_WRITE;
401 } else if (!nasm_stricmp(q, "nowrite")) {
402 flags_and |= SHF_WRITE;
403 flags_or &= ~SHF_WRITE;
404 } else if (!nasm_stricmp(q, "progbits")) {
405 type = SHT_PROGBITS;
406 } else if (!nasm_stricmp(q, "nobits")) {
407 type = SHT_NOBITS;
411 if (!strcmp(name, ".comment") ||
412 !strcmp(name, ".shstrtab") ||
413 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
414 error(ERR_NONFATAL, "attempt to redefine reserved section"
415 "name `%s'", name);
416 return NO_SEG;
419 for (i = 0; i < nsects; i++)
420 if (!strcmp(name, sects[i]->name))
421 break;
422 if (i == nsects) {
423 if (!strcmp(name, ".text"))
424 i = elf_make_section(name, SHT_PROGBITS,
425 SHF_ALLOC | SHF_EXECINSTR, 16);
426 else if (!strcmp(name, ".rodata"))
427 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
428 else if (!strcmp(name, ".data"))
429 i = elf_make_section(name, SHT_PROGBITS,
430 SHF_ALLOC | SHF_WRITE, 4);
431 else if (!strcmp(name, ".bss"))
432 i = elf_make_section(name, SHT_NOBITS,
433 SHF_ALLOC | SHF_WRITE, 4);
434 else
435 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
436 if (type)
437 sects[i]->type = type;
438 if (align)
439 sects[i]->align = align;
440 sects[i]->flags &= ~flags_and;
441 sects[i]->flags |= flags_or;
442 } else if (pass == 1) {
443 if (type || align || flags_and)
444 error(ERR_WARNING, "section attributes ignored on"
445 " redeclaration of section `%s'", name);
448 return sects[i]->index;
451 static void elf_deflabel(char *name, int32_t segment, int32_t offset,
452 int is_global, char *special)
454 int pos = strslen;
455 struct Symbol *sym;
456 bool special_used = false;
458 #if defined(DEBUG) && DEBUG>2
459 fprintf(stderr,
460 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
461 name, segment, offset, is_global, special);
462 #endif
463 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
465 * This is a NASM special symbol. We never allow it into
466 * the ELF symbol table, even if it's a valid one. If it
467 * _isn't_ a valid one, we should barf immediately.
469 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
470 strcmp(name, "..got") && strcmp(name, "..plt") &&
471 strcmp(name, "..sym"))
472 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
473 return;
476 if (is_global == 3) {
477 struct Symbol **s;
479 * Fix up a forward-reference symbol size from the first
480 * pass.
482 for (s = &fwds; *s; s = &(*s)->nextfwd)
483 if (!strcmp((*s)->name, name)) {
484 struct tokenval tokval;
485 expr *e;
486 char *p = special;
488 while (*p && !isspace(*p))
489 p++;
490 while (*p && isspace(*p))
491 p++;
492 stdscan_reset();
493 stdscan_bufptr = p;
494 tokval.t_type = TOKEN_INVALID;
495 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
496 if (e) {
497 if (!is_simple(e))
498 error(ERR_NONFATAL, "cannot use relocatable"
499 " expression as symbol size");
500 else
501 (*s)->size = reloc_value(e);
505 * Remove it from the list of unresolved sizes.
507 nasm_free((*s)->name);
508 *s = (*s)->nextfwd;
509 return;
511 return; /* it wasn't an important one */
514 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
515 strslen += 1 + strlen(name);
517 sym = saa_wstruct(syms);
519 sym->strpos = pos;
520 sym->type = is_global ? SYM_GLOBAL : 0;
521 sym->other = STV_DEFAULT;
522 sym->size = 0;
523 if (segment == NO_SEG)
524 sym->section = SHN_ABS;
525 else {
526 int i;
527 sym->section = SHN_UNDEF;
528 if (nsects == 0 && segment == def_seg) {
529 int tempint;
530 if (segment != elf_section_names(".text", 2, &tempint))
531 error(ERR_PANIC,
532 "strange segment conditions in ELF driver");
533 sym->section = nsects;
534 } else {
535 for (i = 0; i < nsects; i++)
536 if (segment == sects[i]->index) {
537 sym->section = i + 1;
538 break;
543 if (is_global == 2) {
544 sym->size = offset;
545 sym->value = 0;
546 sym->section = SHN_COMMON;
548 * We have a common variable. Check the special text to see
549 * if it's a valid number and power of two; if so, store it
550 * as the alignment for the common variable.
552 if (special) {
553 bool err;
554 sym->value = readnum(special, &err);
555 if (err)
556 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
557 " valid number", special);
558 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
559 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
560 " power of two", special);
562 special_used = true;
563 } else
564 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
566 if (sym->type == SYM_GLOBAL) {
568 * If sym->section == SHN_ABS, then the first line of the
569 * else section would cause a core dump, because its a reference
570 * beyond the end of the section array.
571 * This behaviour is exhibited by this code:
572 * GLOBAL crash_nasm
573 * crash_nasm equ 0
574 * To avoid such a crash, such requests are silently discarded.
575 * This may not be the best solution.
577 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
578 bsym = raa_write(bsym, segment, nglobs);
579 } else if (sym->section != SHN_ABS) {
581 * This is a global symbol; so we must add it to the linked
582 * list of global symbols in its section. We'll push it on
583 * the beginning of the list, because it doesn't matter
584 * much which end we put it on and it's easier like this.
586 * In addition, we check the special text for symbol
587 * type and size information.
589 sym->next = sects[sym->section - 1]->gsyms;
590 sects[sym->section - 1]->gsyms = sym;
592 if (special) {
593 int n = strcspn(special, " \t");
595 if (!nasm_strnicmp(special, "function", n))
596 sym->type |= SYM_FUNCTION;
597 else if (!nasm_strnicmp(special, "data", n) ||
598 !nasm_strnicmp(special, "object", n))
599 sym->type |= SYM_DATA;
600 else if (!nasm_strnicmp(special, "notype", n))
601 sym->type |= SYM_NOTYPE;
602 else
603 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
604 n, special);
605 special += n;
607 while (isspace(*special))
608 ++special;
609 if (*special) {
610 n = strcspn(special, " \t");
611 if (!nasm_strnicmp(special, "default", n))
612 sym->other = STV_DEFAULT;
613 else if (!nasm_strnicmp(special, "internal", n))
614 sym->other = STV_INTERNAL;
615 else if (!nasm_strnicmp(special, "hidden", n))
616 sym->other = STV_HIDDEN;
617 else if (!nasm_strnicmp(special, "protected", n))
618 sym->other = STV_PROTECTED;
619 else
620 n = 0;
621 special += n;
624 if (*special) {
625 struct tokenval tokval;
626 expr *e;
627 int fwd = 0;
628 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
630 while (special[n] && isspace(special[n]))
631 n++;
633 * We have a size expression; attempt to
634 * evaluate it.
636 stdscan_reset();
637 stdscan_bufptr = special + n;
638 tokval.t_type = TOKEN_INVALID;
639 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
640 NULL);
641 if (fwd) {
642 sym->nextfwd = fwds;
643 fwds = sym;
644 sym->name = nasm_strdup(name);
645 } else if (e) {
646 if (!is_simple(e))
647 error(ERR_NONFATAL, "cannot use relocatable"
648 " expression as symbol size");
649 else
650 sym->size = reloc_value(e);
652 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
654 special_used = true;
657 sym->globnum = nglobs;
658 nglobs++;
659 } else
660 nlocals++;
662 if (special && !special_used)
663 error(ERR_NONFATAL, "no special symbol features supported here");
666 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
668 struct Reloc *r;
670 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
671 sect->tail = &r->next;
672 r->next = NULL;
674 r->address = sect->len;
675 if (segment == NO_SEG)
676 r->symbol = 2;
677 else {
678 int i;
679 r->symbol = 0;
680 for (i = 0; i < nsects; i++)
681 if (segment == sects[i]->index)
682 r->symbol = i + 3;
683 if (!r->symbol)
684 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
686 r->type = type;
688 sect->nrelocs++;
692 * This routine deals with ..got and ..sym relocations: the more
693 * complicated kinds. In shared-library writing, some relocations
694 * with respect to global symbols must refer to the precise symbol
695 * rather than referring to an offset from the base of the section
696 * _containing_ the symbol. Such relocations call to this routine,
697 * which searches the symbol list for the symbol in question.
699 * R_386_GOT32 references require the _exact_ symbol address to be
700 * used; R_386_32 references can be at an offset from the symbol.
701 * The boolean argument `exact' tells us this.
703 * Return value is the adjusted value of `addr', having become an
704 * offset from the symbol rather than the section. Should always be
705 * zero when returning from an exact call.
707 * Limitation: if you define two symbols at the same place,
708 * confusion will occur.
710 * Inefficiency: we search, currently, using a linked list which
711 * isn't even necessarily sorted.
713 static int32_t elf_add_gsym_reloc(struct Section *sect,
714 int32_t segment, int64_t offset,
715 int type, bool exact)
717 struct Reloc *r;
718 struct Section *s;
719 struct Symbol *sym, *sm;
720 int i;
723 * First look up the segment/offset pair and find a global
724 * symbol corresponding to it. If it's not one of our segments,
725 * then it must be an external symbol, in which case we're fine
726 * doing a normal elf_add_reloc after first sanity-checking
727 * that the offset from the symbol is zero.
729 s = NULL;
730 for (i = 0; i < nsects; i++)
731 if (segment == sects[i]->index) {
732 s = sects[i];
733 break;
735 if (!s) {
736 if (exact && offset != 0)
737 error(ERR_NONFATAL, "unable to find a suitable global symbol"
738 " for this reference");
739 else
740 elf_add_reloc(sect, segment, type);
741 return offset;
744 if (exact) {
746 * Find a symbol pointing _exactly_ at this one.
748 for (sym = s->gsyms; sym; sym = sym->next)
749 if (sym->value == offset)
750 break;
751 } else {
753 * Find the nearest symbol below this one.
755 sym = NULL;
756 for (sm = s->gsyms; sm; sm = sm->next)
757 if (sm->value <= offset && (!sym || sm->value > sym->value))
758 sym = sm;
760 if (!sym && exact) {
761 error(ERR_NONFATAL, "unable to find a suitable global symbol"
762 " for this reference");
763 return 0;
766 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
767 sect->tail = &r->next;
768 r->next = NULL;
770 r->address = sect->len;
771 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
772 r->type = type;
774 sect->nrelocs++;
776 return offset - sym->value;
779 static void elf_out(int32_t segto, const void *data, uint32_t type,
780 int32_t segment, int32_t wrt)
782 struct Section *s;
783 int32_t realbytes = type & OUT_SIZMASK;
784 int64_t addr;
785 uint8_t mydata[16], *p;
786 int i;
787 static struct symlininfo sinfo;
789 type &= OUT_TYPMASK;
791 #if defined(DEBUG) && DEBUG>2
792 fprintf(stderr,
793 " elf_out type: %x seg: %d bytes: %x data: %x\n",
794 (type >> 24), segment, realbytes, *(int32_t *)data);
795 #endif
798 * handle absolute-assembly (structure definitions)
800 if (segto == NO_SEG) {
801 if (type != OUT_RESERVE)
802 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
803 " space");
804 return;
807 s = NULL;
808 for (i = 0; i < nsects; i++)
809 if (segto == sects[i]->index) {
810 s = sects[i];
811 break;
813 if (!s) {
814 int tempint; /* ignored */
815 if (segto != elf_section_names(".text", 2, &tempint))
816 error(ERR_PANIC, "strange segment conditions in ELF driver");
817 else {
818 s = sects[nsects - 1];
819 i = nsects - 1;
823 /* again some stabs debugging stuff */
824 if (of_elf64.current_dfmt) {
825 sinfo.offset = s->len;
826 sinfo.section = i;
827 sinfo.name = s->name;
828 of_elf64.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
830 /* end of debugging stuff */
832 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
833 error(ERR_WARNING, "attempt to initialize memory in"
834 " BSS section `%s': ignored", s->name);
835 if (type == OUT_REL2ADR)
836 realbytes = 2;
837 else if (type == OUT_REL4ADR)
838 realbytes = 4;
839 s->len += realbytes;
840 return;
843 if (type == OUT_RESERVE) {
844 if (s->type == SHT_PROGBITS) {
845 error(ERR_WARNING, "uninitialized space declared in"
846 " non-BSS section `%s': zeroing", s->name);
847 elf_sect_write(s, NULL, realbytes);
848 } else
849 s->len += realbytes;
850 } else if (type == OUT_RAWDATA) {
851 if (segment != NO_SEG)
852 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
853 elf_sect_write(s, data, realbytes);
854 } else if (type == OUT_ADDRESS) {
855 bool gnu16 = false;
856 addr = *(int64_t *)data;
857 if (segment != NO_SEG) {
858 if (segment % 2) {
859 error(ERR_NONFATAL, "ELF format does not support"
860 " segment base references");
861 } else {
862 if (wrt == NO_SEG) {
863 switch (realbytes) {
864 case 2:
865 elf_add_reloc(s, segment, R_X86_64_16);
866 break;
867 case 4:
868 elf_add_reloc(s, segment, R_X86_64_32);
869 break;
870 case 8:
871 elf_add_reloc(s, segment, R_X86_64_64);
872 break;
873 default:
874 error(ERR_PANIC, "internal error elf64-hpa-871");
875 break;
877 } else if (wrt == elf_gotpc_sect + 1) {
879 * The user will supply GOT relative to $$. ELF
880 * will let us have GOT relative to $. So we
881 * need to fix up the data item by $-$$.
883 addr += s->len;
884 elf_add_reloc(s, segment, R_X86_64_GOTPCREL);
885 } else if (wrt == elf_gotoff_sect + 1) {
886 elf_add_reloc(s, segment, R_X86_64_GOTTPOFF);
887 } else if (wrt == elf_got_sect + 1) {
888 addr = elf_add_gsym_reloc(s, segment, addr,
889 R_X86_64_GOT32, true);
890 } else if (wrt == elf_sym_sect + 1) {
891 switch (realbytes) {
892 case 2:
893 gnu16 = true;
894 addr = elf_add_gsym_reloc(s, segment, addr,
895 R_X86_64_16, false);
896 break;
897 case 4:
898 addr = elf_add_gsym_reloc(s, segment, addr,
899 R_X86_64_32, false);
900 break;
901 case 8:
902 addr = elf_add_gsym_reloc(s, segment, addr,
903 R_X86_64_64, false);
904 break;
905 default:
906 error(ERR_PANIC, "internal error elf64-hpa-903");
907 break;
909 } else if (wrt == elf_plt_sect + 1) {
910 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
911 "relative PLT references");
912 } else {
913 error(ERR_NONFATAL, "ELF format does not support this"
914 " use of WRT");
915 wrt = NO_SEG; /* we can at least _try_ to continue */
919 p = mydata;
920 if (gnu16) {
921 WRITESHORT(p, addr);
922 } else {
923 if (realbytes != 8 && realbytes != 4 && segment != NO_SEG) {
924 error(ERR_NONFATAL,
925 "Unsupported non-64-bit ELF relocation");
927 if (realbytes == 4) WRITELONG(p, addr);
928 else WRITEDLONG(p, (int64_t)addr);
930 elf_sect_write(s, mydata, realbytes);
931 } else if (type == OUT_REL2ADR) {
932 if (segment == segto)
933 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
934 if (segment != NO_SEG && segment % 2) {
935 error(ERR_NONFATAL, "ELF format does not support"
936 " segment base references");
937 } else {
938 if (wrt == NO_SEG) {
939 elf_add_reloc(s, segment, R_X86_64_PC16);
940 } else {
941 error(ERR_NONFATAL,
942 "Unsupported non-32-bit ELF relocation [2]");
945 p = mydata;
946 WRITESHORT(p, *(int32_t *)data - realbytes);
947 elf_sect_write(s, mydata, 2L);
948 } else if (type == OUT_REL4ADR) {
949 if (segment == segto)
950 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
951 if (segment != NO_SEG && segment % 2) {
952 error(ERR_NONFATAL, "ELF format does not support"
953 " segment base references");
954 } else {
955 if (wrt == NO_SEG) {
956 elf_add_reloc(s, segment, R_X86_64_PC32);
957 } else if (wrt == elf_plt_sect + 1) {
958 elf_add_reloc(s, segment, R_X86_64_PLT32);
959 } else if (wrt == elf_gotpc_sect + 1 ||
960 wrt == elf_gotoff_sect + 1 ||
961 wrt == elf_got_sect + 1) {
962 error(ERR_NONFATAL, "ELF format cannot produce PC-"
963 "relative GOT 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 */
970 p = mydata;
971 WRITELONG(p, *(int32_t *)data - realbytes);
972 elf_sect_write(s, mydata, 4L);
976 static void elf_write(void)
978 int nsections, align;
979 int scount;
980 char *p;
981 int commlen;
982 char comment[64];
983 int i;
985 struct SAA *symtab;
986 int32_t symtablen, symtablocal;
989 * Work out how many sections we will have. We have SHN_UNDEF,
990 * then the flexible user sections, then the four fixed
991 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
992 * then optionally relocation sections for the user sections.
994 if (of_elf64.current_dfmt == &df_stabs)
995 nsections = 8;
996 else
997 nsections = 5; /* SHN_UNDEF and the fixed ones */
999 add_sectname("", ".comment");
1000 add_sectname("", ".shstrtab");
1001 add_sectname("", ".symtab");
1002 add_sectname("", ".strtab");
1003 for (i = 0; i < nsects; i++) {
1004 nsections++; /* for the section itself */
1005 if (sects[i]->head) {
1006 nsections++; /* for its relocations without addends*/
1007 add_sectname(".rela", sects[i]->name);
1011 if (of_elf64.current_dfmt == &df_stabs) {
1012 /* in case the debug information is wanted, just add these three sections... */
1013 add_sectname("", ".stab");
1014 add_sectname("", ".stabstr");
1015 add_sectname(".rel", ".stab");
1019 * Do the comment.
1021 *comment = '\0';
1022 commlen =
1023 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1026 * Output the ELF header.
1028 fwrite("\177ELF\2\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
1029 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1030 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1031 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1032 fwriteint64_t(0L, elffp); /* no entry point */
1033 fwriteint64_t(0L, elffp); /* no program header table */
1034 fwriteint64_t(0x40L, elffp); /* section headers straight after
1035 * ELF header plus alignment */
1036 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1037 fwriteint16_t(0x40, elffp); /* size of ELF header */
1038 fwriteint16_t(0, elffp); /* no program header table, again */
1039 fwriteint16_t(0, elffp); /* still no program header table */
1040 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1041 fwriteint16_t(nsections, elffp); /* number of sections */
1042 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1043 * section header table */
1046 * Build the symbol table and relocation tables.
1048 symtab = elf_build_symtab(&symtablen, &symtablocal);
1049 for (i = 0; i < nsects; i++)
1050 if (sects[i]->head)
1051 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1052 sects[i]->head);
1055 * Now output the section header table.
1058 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1059 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1060 elf_foffs += align;
1061 elf_nsect = 0;
1062 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1064 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1065 scount = 1; /* needed for the stabs debugging to track the symtable section */
1066 p = shstrtab + 1;
1067 for (i = 0; i < nsects; i++) {
1068 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1069 (sects[i]->type == SHT_PROGBITS ?
1070 sects[i]->data : NULL), true,
1071 sects[i]->len, 0, 0, sects[i]->align, 0);
1072 p += strlen(p) + 1;
1073 scount++; /* dito */
1075 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1076 scount++; /* dito */
1077 p += strlen(p) + 1;
1078 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1079 scount++; /* dito */
1080 p += strlen(p) + 1;
1081 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1082 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1083 p += strlen(p) + 1;
1084 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1085 for (i = 0; i < nsects; i++)
1086 if (sects[i]->head) {
1087 p += strlen(p) + 1;
1088 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1089 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1091 if (of_elf64.current_dfmt == &df_stabs) {
1092 /* for debugging information, create the last three sections
1093 which are the .stab , .stabstr and .rel.stab sections respectively */
1095 /* this function call creates the stab sections in memory */
1096 stabs64_generate();
1098 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1099 p += strlen(p) + 1;
1100 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1101 nsections - 2, 0, 4, 12);
1103 p += strlen(p) + 1;
1104 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1105 stabstrlen, 0, 0, 4, 0);
1107 p += strlen(p) + 1;
1108 /* link -> symtable info -> section to refer to */
1109 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1110 stabrellen, symtabsection, nsections - 3, 4,
1111 16);
1114 fwrite(align_str, align, 1, elffp);
1117 * Now output the sections.
1119 elf_write_sections();
1121 nasm_free(elf_sects);
1122 saa_free(symtab);
1125 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1127 struct SAA *s = saa_init(1L);
1128 struct Symbol *sym;
1129 uint8_t entry[24], *p;
1130 int i;
1132 *len = *local = 0;
1135 * First, an all-zeros entry, required by the ELF spec.
1137 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1138 *len += 24;
1139 (*local)++;
1142 * Next, an entry for the file name.
1144 p = entry;
1145 WRITELONG(p, 1); /* we know it's 1st thing in strtab */
1146 WRITESHORT(p, 4); /* type FILE */
1147 WRITESHORT(p, SHN_ABS);
1148 WRITEDLONG(p, (uint64_t) 0); /* no value */
1149 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1150 saa_wbytes(s, entry, 24L);
1151 *len += 24;
1152 (*local)++;
1155 * Now some standard symbols defining the segments, for relocation
1156 * purposes.
1158 for (i = 1; i <= nsects + 1; i++) {
1159 p = entry;
1160 WRITELONG(p, 0); /* no symbol name */
1161 WRITESHORT(p, 3); /* local section-type thing */
1162 WRITESHORT(p, (i == 1 ? SHN_ABS : i - 1)); /* the section id */
1163 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1164 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1165 saa_wbytes(s, entry, 24L);
1166 *len += 24;
1167 (*local)++;
1171 * Now the other local symbols.
1173 saa_rewind(syms);
1174 while ((sym = saa_rstruct(syms))) {
1175 if (sym->type & SYM_GLOBAL)
1176 continue;
1177 p = entry;
1178 WRITELONG(p, sym->strpos);
1179 WRITECHAR(p, sym->type); /* local non-typed thing */
1180 WRITECHAR(p, sym->other);
1181 WRITESHORT(p, sym->section);
1182 WRITEDLONG(p, (int64_t)sym->value);
1183 WRITEDLONG(p, (int64_t)sym->size);
1184 saa_wbytes(s, entry, 24L);
1185 *len += 24;
1186 (*local)++;
1190 * Now the global symbols.
1192 saa_rewind(syms);
1193 while ((sym = saa_rstruct(syms))) {
1194 if (!(sym->type & SYM_GLOBAL))
1195 continue;
1196 p = entry;
1197 WRITELONG(p, sym->strpos);
1198 WRITECHAR(p, sym->type); /* global non-typed thing */
1199 WRITECHAR(p, sym->other);
1200 WRITESHORT(p, sym->section);
1201 WRITEDLONG(p, (int64_t)sym->value);
1202 WRITEDLONG(p, (int64_t)sym->size);
1203 saa_wbytes(s, entry, 24L);
1204 *len += 24;
1207 return s;
1210 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1212 struct SAA *s;
1213 uint8_t *p, entry[24];
1215 if (!r)
1216 return NULL;
1218 s = saa_init(1L);
1219 *len = 0;
1221 while (r) {
1222 int64_t sym = r->symbol;
1224 if (sym >= GLOBAL_TEMP_BASE)
1225 sym += -GLOBAL_TEMP_BASE + (nsects + 3) + nlocals;
1227 p = entry;
1228 WRITEDLONG(p, r->address);
1229 WRITEDLONG(p, (sym << 32) + r->type);
1230 WRITEDLONG(p, (uint64_t) 0);
1231 saa_wbytes(s, entry, 24L);
1232 *len += 24;
1234 r = r->next;
1237 return s;
1240 static void elf_section_header(int name, int type, int flags,
1241 void *data, bool is_saa, int32_t datalen,
1242 int link, int info, int align, int eltsize)
1244 elf_sects[elf_nsect].data = data;
1245 elf_sects[elf_nsect].len = datalen;
1246 elf_sects[elf_nsect].is_saa = is_saa;
1247 elf_nsect++;
1249 fwriteint32_t((int32_t)name, elffp);
1250 fwriteint32_t((int32_t)type, elffp);
1251 fwriteint64_t((int64_t)flags, elffp);
1252 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1253 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1254 fwriteint64_t(datalen, elffp);
1255 if (data)
1256 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1257 fwriteint32_t((int32_t)link, elffp);
1258 fwriteint32_t((int32_t)info, elffp);
1259 fwriteint64_t((int64_t)align, elffp);
1260 fwriteint64_t((int64_t)eltsize, elffp);
1263 static void elf_write_sections(void)
1265 int i;
1266 for (i = 0; i < elf_nsect; i++)
1267 if (elf_sects[i].data) {
1268 int32_t len = elf_sects[i].len;
1269 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1270 int32_t align = reallen - len;
1271 if (elf_sects[i].is_saa)
1272 saa_fpwrite(elf_sects[i].data, elffp);
1273 else
1274 fwrite(elf_sects[i].data, len, 1, elffp);
1275 fwrite(align_str, align, 1, elffp);
1279 static void elf_sect_write(struct Section *sect,
1280 const uint8_t *data, uint32_t len)
1282 saa_wbytes(sect->data, data, len);
1283 sect->len += len;
1286 static int32_t elf_segbase(int32_t segment)
1288 return segment;
1291 static int elf_directive(char *directive, char *value, int pass)
1293 (void)directive;
1294 (void)value;
1295 (void)pass;
1296 return 0;
1299 static void elf_filename(char *inname, char *outname, efunc error)
1301 strcpy(elf_module, inname);
1302 standard_extension(inname, outname, ".o", error);
1305 static const char *elf_stdmac[] = {
1306 "%define __SECT__ [section .text]",
1307 "%macro __NASM_CDecl__ 1",
1308 "%define $_%1 $%1",
1309 "%endmacro",
1310 NULL
1312 static int elf_set_info(enum geninfo type, char **val)
1314 (void)type;
1315 (void)val;
1316 return 0;
1319 static struct dfmt df_stabs = {
1320 "ELF64 (X86_64) stabs debug format for Linux",
1321 "stabs",
1322 stabs64_init,
1323 stabs64_linenum,
1324 stabs64_deflabel,
1325 stabs64_directive,
1326 stabs64_typevalue,
1327 stabs64_output,
1328 stabs64_cleanup
1331 struct dfmt *elf64_debugs_arr[2] = { &df_stabs, NULL };
1333 struct ofmt of_elf64 = {
1334 "ELF64 (x86_64) object files (e.g. Linux)",
1335 "elf64",
1336 NULL,
1337 elf64_debugs_arr,
1338 &null_debug_form,
1339 elf_stdmac,
1340 elf_init,
1341 elf_set_info,
1342 elf_out,
1343 elf_deflabel,
1344 elf_section_names,
1345 elf_segbase,
1346 elf_directive,
1347 elf_filename,
1348 elf_cleanup
1351 /* again, the stabs debugging stuff (code) */
1353 void stabs64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1355 (void)of;
1356 (void)id;
1357 (void)fp;
1358 (void)error;
1361 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1363 (void)segto;
1365 if (!stabs_filename) {
1366 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1367 strcpy(stabs_filename, filename);
1368 } else {
1369 if (strcmp(stabs_filename, filename)) {
1370 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1371 in fact, this leak comes in quite handy to maintain a list of files
1372 encountered so far in the symbol lines... */
1374 /* why not nasm_free(stabs_filename); we're done with the old one */
1376 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1377 strcpy(stabs_filename, filename);
1380 stabs_immcall = 1;
1381 currentline = linenumber;
1384 void stabs64_deflabel(char *name, int32_t segment, int32_t offset, int is_global,
1385 char *special)
1387 (void)name;
1388 (void)segment;
1389 (void)offset;
1390 (void)is_global;
1391 (void)special;
1394 void stabs64_directive(const char *directive, const char *params)
1396 (void)directive;
1397 (void)params;
1400 void stabs64_typevalue(int32_t type)
1402 (void)type;
1405 void stabs64_output(int type, void *param)
1407 struct symlininfo *s;
1408 struct linelist *el;
1409 if (type == TY_STABSSYMLIN) {
1410 if (stabs_immcall) {
1411 s = (struct symlininfo *)param;
1412 if (strcmp(s->name, ".text"))
1413 return; /* we are only interested in the text stuff */
1414 numlinestabs++;
1415 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1416 el->info.offset = s->offset;
1417 el->info.section = s->section;
1418 el->info.name = s->name;
1419 el->line = currentline;
1420 el->filename = stabs_filename;
1421 el->next = 0;
1422 if (stabslines) {
1423 stabslines->last->next = el;
1424 stabslines->last = el;
1425 } else {
1426 stabslines = el;
1427 stabslines->last = el;
1431 stabs_immcall = 0;
1434 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1435 do {\
1436 WRITELONG(p,n_strx); \
1437 WRITECHAR(p,n_type); \
1438 WRITECHAR(p,n_other); \
1439 WRITESHORT(p,n_desc); \
1440 WRITELONG(p,n_value); \
1441 } while (0)
1443 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1445 void stabs64_generate(void)
1447 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1448 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1449 char **allfiles;
1450 int *fileidx;
1452 struct linelist *ptr;
1454 ptr = stabslines;
1456 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1457 for (i = 0; i < numlinestabs; i++)
1458 allfiles[i] = 0;
1459 numfiles = 0;
1460 while (ptr) {
1461 if (numfiles == 0) {
1462 allfiles[0] = ptr->filename;
1463 numfiles++;
1464 } else {
1465 for (i = 0; i < numfiles; i++) {
1466 if (!strcmp(allfiles[i], ptr->filename))
1467 break;
1469 if (i >= numfiles) {
1470 allfiles[i] = ptr->filename;
1471 numfiles++;
1474 ptr = ptr->next;
1476 strsize = 1;
1477 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1478 for (i = 0; i < numfiles; i++) {
1479 fileidx[i] = strsize;
1480 strsize += strlen(allfiles[i]) + 1;
1482 mainfileindex = 0;
1483 for (i = 0; i < numfiles; i++) {
1484 if (!strcmp(allfiles[i], elf_module)) {
1485 mainfileindex = i;
1486 break;
1490 /* worst case size of the stab buffer would be:
1491 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1493 sbuf =
1494 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1495 sizeof(struct stabentry));
1497 ssbuf = (uint8_t *)nasm_malloc(strsize);
1499 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1500 rptr = rbuf;
1502 for (i = 0; i < numfiles; i++) {
1503 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1505 ssbuf[0] = 0;
1507 stabstrlen = strsize; /* set global variable for length of stab strings */
1509 sptr = sbuf;
1510 /* this is the first stab, its strx points to the filename of the
1511 the source-file, the n_desc field should be set to the number
1512 of remaining stabs
1514 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1516 ptr = stabslines;
1517 numstabs = 0;
1519 if (ptr) {
1520 /* this is the stab for the main source file */
1521 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1523 /* relocation table entry */
1525 /* Since the above WRITE_STAB calls have already */
1526 /* created two entries, the index in the info.section */
1527 /* member must be adjusted by adding 3 */
1529 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1530 WRITELONG(rptr, R_X86_64_32);
1531 WRITELONG(rptr, ptr->info.section + 3);
1533 numstabs++;
1534 currfile = mainfileindex;
1537 while (ptr) {
1538 if (strcmp(allfiles[currfile], ptr->filename)) {
1539 /* oops file has changed... */
1540 for (i = 0; i < numfiles; i++)
1541 if (!strcmp(allfiles[i], ptr->filename))
1542 break;
1543 currfile = i;
1544 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1545 ptr->info.offset);
1546 numstabs++;
1548 /* relocation table entry */
1550 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1551 WRITELONG(rptr, R_X86_64_32);
1552 WRITELONG(rptr, ptr->info.section + 3);
1555 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1556 numstabs++;
1558 /* relocation table entry */
1560 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1561 WRITELONG(rptr, R_X86_64_32);
1562 WRITELONG(rptr, ptr->info.section + 3);
1564 ptr = ptr->next;
1568 ((struct stabentry *)sbuf)->n_desc = numstabs;
1570 nasm_free(allfiles);
1571 nasm_free(fileidx);
1573 stablen = (sptr - sbuf);
1574 stabrellen = (rptr - rbuf);
1575 stabrelbuf = rbuf;
1576 stabbuf = sbuf;
1577 stabstrbuf = ssbuf;
1580 void stabs64_cleanup(void)
1582 struct linelist *ptr, *del;
1583 if (!stabslines)
1584 return;
1585 ptr = stabslines;
1586 while (ptr) {
1587 del = ptr;
1588 ptr = ptr->next;
1589 nasm_free(del);
1591 if (stabbuf)
1592 nasm_free(stabbuf);
1593 if (stabrelbuf)
1594 nasm_free(stabrelbuf);
1595 if (stabstrbuf)
1596 nasm_free(stabstrbuf);
1599 #endif /* OF_ELF */