Provide 64-bit support for ORG directive
[nasm/avx512.git] / output / outelf64.c
blob554ed5e52eaaaa5821f25ca324c0e5227412ab3c
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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <inttypes.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "stdscan.h"
19 #include "outform.h"
21 /* Definitions in lieu of elf.h */
23 #define SHT_PROGBITS 1
24 #define SHT_RELA 4 /* Relocation entries with addends */
25 #define SHT_NOBITS 8
26 #define SHF_WRITE (1 << 0) /* Writable */
27 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
28 #define SHF_EXECINSTR (1 << 2) /* Executable */
29 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
30 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
31 #define R_X86_64_NONE 0 /* No reloc */
32 #define R_X86_64_64 1 /* Direct 64 bit address */
33 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
34 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
35 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
36 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
37 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
38 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
39 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
40 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
41 #define ET_REL 1 /* Relocatable file */
42 #define EM_X86_64 62 /* AMD x86-64 architecture */
43 typedef uint32_t Elf64_Word;
44 typedef uint64_t Elf64_Xword;
45 typedef uint64_t Elf64_Addr;
46 typedef uint64_t Elf64_Off;
47 typedef struct
49 Elf64_Word sh_name; /* Section name (string tbl index) */
50 Elf64_Word sh_type; /* Section type */
51 Elf64_Xword sh_flags; /* Section flags */
52 Elf64_Addr sh_addr; /* Section virtual addr at execution */
53 Elf64_Off sh_offset; /* Section file offset */
54 Elf64_Xword sh_size; /* Section size in bytes */
55 Elf64_Word sh_link; /* Link to another section */
56 Elf64_Word sh_info; /* Additional section information */
57 Elf64_Xword sh_addralign; /* Section alignment */
58 Elf64_Xword sh_entsize; /* Entry size if section holds table */
59 } Elf64_Shdr;
62 #ifdef OF_ELF64
65 struct Reloc {
66 struct Reloc *next;
67 int64_t address; /* relative to _start_ of section */
68 int64_t symbol; /* ELF symbol info thingy */
69 int type; /* type of relocation */
72 struct Symbol {
73 int32_t strpos; /* string table position of name */
74 int32_t section; /* section ID of the symbol */
75 int type; /* symbol type */
76 int other; /* symbol visibility */
77 int32_t value; /* address, or COMMON variable align */
78 int32_t size; /* size of symbol */
79 int32_t globnum; /* symbol table offset if global */
80 struct Symbol *next; /* list of globals in each section */
81 struct Symbol *nextfwd; /* list of unresolved-size symbols */
82 char *name; /* used temporarily if in above list */
86 struct Section {
87 struct SAA *data;
88 uint32_t len, size, nrelocs;
89 int32_t index;
90 int type; /* SHT_PROGBITS or SHT_NOBITS */
91 int align; /* alignment: power of two */
92 uint32_t flags; /* section flags */
93 char *name;
94 struct SAA *rel;
95 int32_t rellen;
96 struct Reloc *head, **tail;
97 struct Symbol *gsyms; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section **sects;
102 static int nsects, sectlen;
104 #define SHSTR_DELTA 256
105 static char *shstrtab;
106 static int shstrtablen, shstrtabsize;
108 static struct SAA *syms;
109 static uint32_t nlocals, nglobs;
111 static int32_t def_seg;
113 static struct RAA *bsym;
115 static struct SAA *strs;
116 static uint32_t strslen;
118 static FILE *elffp;
119 static efunc error;
120 static evalfunc evaluate;
122 static struct Symbol *fwds;
124 static char elf_module[FILENAME_MAX];
126 extern struct ofmt of_elf64;
128 #define SHN_UNDEF 0
130 #define SYM_SECTION 0x04
131 #define SYM_GLOBAL 0x10
132 #define SYM_NOTYPE 0x00
133 #define SYM_DATA 0x01
134 #define SYM_FUNCTION 0x02
136 #define STV_DEFAULT 0
137 #define STV_INTERNAL 1
138 #define STV_HIDDEN 2
139 #define STV_PROTECTED 3
141 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
143 #define SEG_ALIGN 16 /* alignment of sections in file */
144 #define SEG_ALIGN_1 (SEG_ALIGN-1)
146 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
148 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
149 static struct ELF_SECTDATA {
150 void *data;
151 int32_t len;
152 int is_saa;
153 } *elf_sects;
154 static int elf_nsect;
155 static int32_t elf_foffs;
157 static void elf_write(void);
158 static void elf_sect_write(struct Section *, const uint8_t *,
159 uint32_t);
160 static void elf_section_header(int, int, int, void *, int, int32_t, int, int,
161 int, int);
162 static void elf_write_sections(void);
163 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
164 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
165 static void add_sectname(char *, char *);
167 /* this stuff is needed for the stabs debugging format */
168 #define N_SO 0x64 /* ID for main source file */
169 #define N_SOL 0x84 /* ID for sub-source file */
170 #define N_BINCL 0x82
171 #define N_EINCL 0xA2
172 #define N_SLINE 0x44
173 #define TY_STABSSYMLIN 0x40 /* ouch */
175 struct stabentry {
176 uint32_t n_strx;
177 uint8_t n_type;
178 uint8_t n_other;
179 uint16_t n_desc;
180 uint32_t n_value;
183 struct erel {
184 int offset, info;
187 struct symlininfo {
188 int offset;
189 int section; /* section index */
190 char *name; /* shallow-copied pointer of section name */
193 struct linelist {
194 struct symlininfo info;
195 int line;
196 char *filename;
197 struct linelist *next;
198 struct linelist *last;
201 static struct linelist *stabslines = 0;
202 static int stabs_immcall = 0;
203 static int currentline = 0;
204 static int numlinestabs = 0;
205 static char *stabs_filename = 0;
206 static int symtabsection;
207 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
208 static int stablen, stabstrlen, stabrellen;
210 static struct dfmt df_stabs;
212 void stabs64_init(struct ofmt *, void *, FILE *, efunc);
213 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
214 void stabs64_deflabel(char *, int32_t, int32_t, int, char *);
215 void stabs64_directive(const char *, const char *);
216 void stabs64_typevalue(int32_t);
217 void stabs64_output(int, void *);
218 void stabs64_generate();
219 void stabs64_cleanup();
221 /* end of stabs debugging stuff */
224 * Special section numbers which are used to define ELF special
225 * symbols, which can be used with WRT to provide PIC relocation
226 * types.
228 static int32_t elf_gotpc_sect, elf_gotoff_sect;
229 static int32_t elf_got_sect, elf_plt_sect;
230 static int32_t elf_sym_sect;
232 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
234 maxbits = 64;
235 elffp = fp;
236 error = errfunc;
237 evaluate = eval;
238 (void)ldef; /* placate optimisers */
239 sects = NULL;
240 nsects = sectlen = 0;
241 syms = saa_init((int32_t)sizeof(struct Symbol));
242 nlocals = nglobs = 0;
243 bsym = raa_init();
244 strs = saa_init(1L);
245 saa_wbytes(strs, "\0", 1L);
246 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
247 strslen = 2 + strlen(elf_module);
248 shstrtab = NULL;
249 shstrtablen = shstrtabsize = 0;;
250 add_sectname("", "");
252 fwds = NULL;
254 elf_gotpc_sect = seg_alloc();
255 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
256 error);
257 elf_gotoff_sect = seg_alloc();
258 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
259 error);
260 elf_got_sect = seg_alloc();
261 ldef("..got", elf_got_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
262 error);
263 elf_plt_sect = seg_alloc();
264 ldef("..plt", elf_plt_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
265 error);
266 elf_sym_sect = seg_alloc();
267 ldef("..sym", elf_sym_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
268 error);
270 def_seg = seg_alloc();
273 static void elf_cleanup(int debuginfo)
275 struct Reloc *r;
276 int i;
278 (void)debuginfo;
280 elf_write();
281 fclose(elffp);
282 for (i = 0; i < nsects; i++) {
283 if (sects[i]->type != SHT_NOBITS)
284 saa_free(sects[i]->data);
285 if (sects[i]->head)
286 saa_free(sects[i]->rel);
287 while (sects[i]->head) {
288 r = sects[i]->head;
289 sects[i]->head = sects[i]->head->next;
290 nasm_free(r);
293 nasm_free(sects);
294 saa_free(syms);
295 raa_free(bsym);
296 saa_free(strs);
297 if (of_elf64.current_dfmt) {
298 of_elf64.current_dfmt->cleanup();
302 static void add_sectname(char *firsthalf, char *secondhalf)
304 int len = strlen(firsthalf) + strlen(secondhalf);
305 while (shstrtablen + len + 1 > shstrtabsize)
306 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
307 strcpy(shstrtab + shstrtablen, firsthalf);
308 strcat(shstrtab + shstrtablen, secondhalf);
309 shstrtablen += len + 1;
312 static int elf_make_section(char *name, int type, int flags, int align)
314 struct Section *s;
316 s = nasm_malloc(sizeof(*s));
318 if (type != SHT_NOBITS)
319 s->data = saa_init(1L);
320 s->head = NULL;
321 s->tail = &s->head;
322 s->len = s->size = 0;
323 s->nrelocs = 0;
324 if (!strcmp(name, ".text"))
325 s->index = def_seg;
326 else
327 s->index = seg_alloc();
328 add_sectname("", name);
329 s->name = nasm_malloc(1 + strlen(name));
330 strcpy(s->name, name);
331 s->type = type;
332 s->flags = flags;
333 s->align = align;
334 s->gsyms = NULL;
336 if (nsects >= sectlen)
337 sects =
338 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
339 sects[nsects++] = s;
341 return nsects - 1;
344 static int32_t elf_section_names(char *name, int pass, int *bits)
346 char *p;
347 int flags_and, flags_or, type, align, i;
350 * Default is 64 bits.
352 if (!name) {
353 *bits = 64;
354 return def_seg;
357 p = name;
358 while (*p && !isspace(*p))
359 p++;
360 if (*p)
361 *p++ = '\0';
362 flags_and = flags_or = type = align = 0;
364 while (*p && isspace(*p))
365 p++;
366 while (*p) {
367 char *q = p;
368 while (*p && !isspace(*p))
369 p++;
370 if (*p)
371 *p++ = '\0';
372 while (*p && isspace(*p))
373 p++;
375 if (!nasm_strnicmp(q, "align=", 6)) {
376 align = atoi(q + 6);
377 if (align == 0)
378 align = 1;
379 if ((align - 1) & align) { /* means it's not a power of two */
380 error(ERR_NONFATAL, "section alignment %d is not"
381 " a power of two", align);
382 align = 1;
384 } else if (!nasm_stricmp(q, "alloc")) {
385 flags_and |= SHF_ALLOC;
386 flags_or |= SHF_ALLOC;
387 } else if (!nasm_stricmp(q, "noalloc")) {
388 flags_and |= SHF_ALLOC;
389 flags_or &= ~SHF_ALLOC;
390 } else if (!nasm_stricmp(q, "exec")) {
391 flags_and |= SHF_EXECINSTR;
392 flags_or |= SHF_EXECINSTR;
393 } else if (!nasm_stricmp(q, "noexec")) {
394 flags_and |= SHF_EXECINSTR;
395 flags_or &= ~SHF_EXECINSTR;
396 } else if (!nasm_stricmp(q, "write")) {
397 flags_and |= SHF_WRITE;
398 flags_or |= SHF_WRITE;
399 } else if (!nasm_stricmp(q, "nowrite")) {
400 flags_and |= SHF_WRITE;
401 flags_or &= ~SHF_WRITE;
402 } else if (!nasm_stricmp(q, "progbits")) {
403 type = SHT_PROGBITS;
404 } else if (!nasm_stricmp(q, "nobits")) {
405 type = SHT_NOBITS;
409 if (!strcmp(name, ".comment") ||
410 !strcmp(name, ".shstrtab") ||
411 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
412 error(ERR_NONFATAL, "attempt to redefine reserved section"
413 "name `%s'", name);
414 return NO_SEG;
417 for (i = 0; i < nsects; i++)
418 if (!strcmp(name, sects[i]->name))
419 break;
420 if (i == nsects) {
421 if (!strcmp(name, ".text"))
422 i = elf_make_section(name, SHT_PROGBITS,
423 SHF_ALLOC | SHF_EXECINSTR, 16);
424 else if (!strcmp(name, ".rodata"))
425 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
426 else if (!strcmp(name, ".data"))
427 i = elf_make_section(name, SHT_PROGBITS,
428 SHF_ALLOC | SHF_WRITE, 4);
429 else if (!strcmp(name, ".bss"))
430 i = elf_make_section(name, SHT_NOBITS,
431 SHF_ALLOC | SHF_WRITE, 4);
432 else
433 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
434 if (type)
435 sects[i]->type = type;
436 if (align)
437 sects[i]->align = align;
438 sects[i]->flags &= ~flags_and;
439 sects[i]->flags |= flags_or;
440 } else if (pass == 1) {
441 if (type || align || flags_and)
442 error(ERR_WARNING, "section attributes ignored on"
443 " redeclaration of section `%s'", name);
446 return sects[i]->index;
449 static void elf_deflabel(char *name, int32_t segment, int32_t offset,
450 int is_global, char *special)
452 int pos = strslen;
453 struct Symbol *sym;
454 int special_used = FALSE;
456 #if defined(DEBUG) && DEBUG>2
457 fprintf(stderr,
458 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
459 name, segment, offset, is_global, special);
460 #endif
461 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
463 * This is a NASM special symbol. We never allow it into
464 * the ELF symbol table, even if it's a valid one. If it
465 * _isn't_ a valid one, we should barf immediately.
467 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
468 strcmp(name, "..got") && strcmp(name, "..plt") &&
469 strcmp(name, "..sym"))
470 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
471 return;
474 if (is_global == 3) {
475 struct Symbol **s;
477 * Fix up a forward-reference symbol size from the first
478 * pass.
480 for (s = &fwds; *s; s = &(*s)->nextfwd)
481 if (!strcmp((*s)->name, name)) {
482 struct tokenval tokval;
483 expr *e;
484 char *p = special;
486 while (*p && !isspace(*p))
487 p++;
488 while (*p && isspace(*p))
489 p++;
490 stdscan_reset();
491 stdscan_bufptr = p;
492 tokval.t_type = TOKEN_INVALID;
493 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
494 if (e) {
495 if (!is_simple(e))
496 error(ERR_NONFATAL, "cannot use relocatable"
497 " expression as symbol size");
498 else
499 (*s)->size = reloc_value(e);
503 * Remove it from the list of unresolved sizes.
505 nasm_free((*s)->name);
506 *s = (*s)->nextfwd;
507 return;
509 return; /* it wasn't an important one */
512 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
513 strslen += 1 + strlen(name);
515 sym = saa_wstruct(syms);
517 sym->strpos = pos;
518 sym->type = is_global ? SYM_GLOBAL : 0;
519 sym->other = STV_DEFAULT;
520 sym->size = 0;
521 if (segment == NO_SEG)
522 sym->section = SHN_ABS;
523 else {
524 int i;
525 sym->section = SHN_UNDEF;
526 if (nsects == 0 && segment == def_seg) {
527 int tempint;
528 if (segment != elf_section_names(".text", 2, &tempint))
529 error(ERR_PANIC,
530 "strange segment conditions in ELF driver");
531 sym->section = nsects;
532 } else {
533 for (i = 0; i < nsects; i++)
534 if (segment == sects[i]->index) {
535 sym->section = i + 1;
536 break;
541 if (is_global == 2) {
542 sym->size = offset;
543 sym->value = 0;
544 sym->section = SHN_COMMON;
546 * We have a common variable. Check the special text to see
547 * if it's a valid number and power of two; if so, store it
548 * as the alignment for the common variable.
550 if (special) {
551 int err;
552 sym->value = readnum(special, &err);
553 if (err)
554 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
555 " valid number", special);
556 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
557 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
558 " power of two", special);
560 special_used = TRUE;
561 } else
562 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
564 if (sym->type == SYM_GLOBAL) {
566 * If sym->section == SHN_ABS, then the first line of the
567 * else section would cause a core dump, because its a reference
568 * beyond the end of the section array.
569 * This behaviour is exhibited by this code:
570 * GLOBAL crash_nasm
571 * crash_nasm equ 0
572 * To avoid such a crash, such requests are silently discarded.
573 * This may not be the best solution.
575 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
576 bsym = raa_write(bsym, segment, nglobs);
577 } else if (sym->section != SHN_ABS) {
579 * This is a global symbol; so we must add it to the linked
580 * list of global symbols in its section. We'll push it on
581 * the beginning of the list, because it doesn't matter
582 * much which end we put it on and it's easier like this.
584 * In addition, we check the special text for symbol
585 * type and size information.
587 sym->next = sects[sym->section - 1]->gsyms;
588 sects[sym->section - 1]->gsyms = sym;
590 if (special) {
591 int n = strcspn(special, " \t");
593 if (!nasm_strnicmp(special, "function", n))
594 sym->type |= SYM_FUNCTION;
595 else if (!nasm_strnicmp(special, "data", n) ||
596 !nasm_strnicmp(special, "object", n))
597 sym->type |= SYM_DATA;
598 else if (!nasm_strnicmp(special, "notype", n))
599 sym->type |= SYM_NOTYPE;
600 else
601 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
602 n, special);
603 special += n;
605 while (isspace(*special))
606 ++special;
607 if (*special) {
608 n = strcspn(special, " \t");
609 if (!nasm_strnicmp(special, "default", n))
610 sym->other = STV_DEFAULT;
611 else if (!nasm_strnicmp(special, "internal", n))
612 sym->other = STV_INTERNAL;
613 else if (!nasm_strnicmp(special, "hidden", n))
614 sym->other = STV_HIDDEN;
615 else if (!nasm_strnicmp(special, "protected", n))
616 sym->other = STV_PROTECTED;
617 else
618 n = 0;
619 special += n;
622 if (*special) {
623 struct tokenval tokval;
624 expr *e;
625 int fwd = FALSE;
626 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
628 while (special[n] && isspace(special[n]))
629 n++;
631 * We have a size expression; attempt to
632 * evaluate it.
634 stdscan_reset();
635 stdscan_bufptr = special + n;
636 tokval.t_type = TOKEN_INVALID;
637 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
638 NULL);
639 if (fwd) {
640 sym->nextfwd = fwds;
641 fwds = sym;
642 sym->name = nasm_strdup(name);
643 } else if (e) {
644 if (!is_simple(e))
645 error(ERR_NONFATAL, "cannot use relocatable"
646 " expression as symbol size");
647 else
648 sym->size = reloc_value(e);
650 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
652 special_used = TRUE;
655 sym->globnum = nglobs;
656 nglobs++;
657 } else
658 nlocals++;
660 if (special && !special_used)
661 error(ERR_NONFATAL, "no special symbol features supported here");
664 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
666 struct Reloc *r;
668 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
669 sect->tail = &r->next;
670 r->next = NULL;
672 r->address = sect->len;
673 if (segment == NO_SEG)
674 r->symbol = 2;
675 else {
676 int i;
677 r->symbol = 0;
678 for (i = 0; i < nsects; i++)
679 if (segment == sects[i]->index)
680 r->symbol = i + 3;
681 if (!r->symbol)
682 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
684 r->type = type;
686 sect->nrelocs++;
690 * This routine deals with ..got and ..sym relocations: the more
691 * complicated kinds. In shared-library writing, some relocations
692 * with respect to global symbols must refer to the precise symbol
693 * rather than referring to an offset from the base of the section
694 * _containing_ the symbol. Such relocations call to this routine,
695 * which searches the symbol list for the symbol in question.
697 * R_386_GOT32 references require the _exact_ symbol address to be
698 * used; R_386_32 references can be at an offset from the symbol.
699 * The boolean argument `exact' tells us this.
701 * Return value is the adjusted value of `addr', having become an
702 * offset from the symbol rather than the section. Should always be
703 * zero when returning from an exact call.
705 * Limitation: if you define two symbols at the same place,
706 * confusion will occur.
708 * Inefficiency: we search, currently, using a linked list which
709 * isn't even necessarily sorted.
711 static int32_t elf_add_gsym_reloc(struct Section *sect,
712 int32_t segment, int32_t offset,
713 int type, int exact)
715 struct Reloc *r;
716 struct Section *s;
717 struct Symbol *sym, *sm;
718 int i;
721 * First look up the segment/offset pair and find a global
722 * symbol corresponding to it. If it's not one of our segments,
723 * then it must be an external symbol, in which case we're fine
724 * doing a normal elf_add_reloc after first sanity-checking
725 * that the offset from the symbol is zero.
727 s = NULL;
728 for (i = 0; i < nsects; i++)
729 if (segment == sects[i]->index) {
730 s = sects[i];
731 break;
733 if (!s) {
734 if (exact && offset != 0)
735 error(ERR_NONFATAL, "unable to find a suitable global symbol"
736 " for this reference");
737 else
738 elf_add_reloc(sect, segment, type);
739 return offset;
742 if (exact) {
744 * Find a symbol pointing _exactly_ at this one.
746 for (sym = s->gsyms; sym; sym = sym->next)
747 if (sym->value == offset)
748 break;
749 } else {
751 * Find the nearest symbol below this one.
753 sym = NULL;
754 for (sm = s->gsyms; sm; sm = sm->next)
755 if (sm->value <= offset && (!sym || sm->value > sym->value))
756 sym = sm;
758 if (!sym && exact) {
759 error(ERR_NONFATAL, "unable to find a suitable global symbol"
760 " for this reference");
761 return 0;
764 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
765 sect->tail = &r->next;
766 r->next = NULL;
768 r->address = sect->len;
769 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
770 r->type = type;
772 sect->nrelocs++;
774 return offset - sym->value;
777 static void elf_out(int32_t segto, const void *data, uint32_t type,
778 int32_t segment, int32_t wrt)
780 struct Section *s;
781 int32_t realbytes = type & OUT_SIZMASK;
782 int32_t addr;
783 uint8_t mydata[16], *p;
784 int i;
785 static struct symlininfo sinfo;
787 type &= OUT_TYPMASK;
789 #if defined(DEBUG) && DEBUG>2
790 fprintf(stderr,
791 " elf_out type: %x seg: %d bytes: %x data: %x\n",
792 (type >> 24), segment, realbytes, *(int32_t *)data);
793 #endif
796 * handle absolute-assembly (structure definitions)
798 if (segto == NO_SEG) {
799 if (type != OUT_RESERVE)
800 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
801 " space");
802 return;
805 s = NULL;
806 for (i = 0; i < nsects; i++)
807 if (segto == sects[i]->index) {
808 s = sects[i];
809 break;
811 if (!s) {
812 int tempint; /* ignored */
813 if (segto != elf_section_names(".text", 2, &tempint))
814 error(ERR_PANIC, "strange segment conditions in ELF driver");
815 else {
816 s = sects[nsects - 1];
817 i = nsects - 1;
821 /* again some stabs debugging stuff */
822 if (of_elf64.current_dfmt) {
823 sinfo.offset = s->len;
824 sinfo.section = i;
825 sinfo.name = s->name;
826 of_elf64.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
828 /* end of debugging stuff */
830 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
831 error(ERR_WARNING, "attempt to initialize memory in"
832 " BSS section `%s': ignored", s->name);
833 if (type == OUT_REL2ADR)
834 realbytes = 2;
835 else if (type == OUT_REL4ADR)
836 realbytes = 4;
837 s->len += realbytes;
838 return;
841 if (type == OUT_RESERVE) {
842 if (s->type == SHT_PROGBITS) {
843 error(ERR_WARNING, "uninitialized space declared in"
844 " non-BSS section `%s': zeroing", s->name);
845 elf_sect_write(s, NULL, realbytes);
846 } else
847 s->len += realbytes;
848 } else if (type == OUT_RAWDATA) {
849 if (segment != NO_SEG)
850 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
851 elf_sect_write(s, data, realbytes);
852 } else if (type == OUT_ADDRESS) {
853 int gnu16 = 0;
854 addr = *(int32_t *)data;
855 if (segment != NO_SEG) {
856 if (segment % 2) {
857 error(ERR_NONFATAL, "ELF format does not support"
858 " segment base references");
859 } else {
860 if (wrt == NO_SEG) {
861 switch (realbytes) {
862 case 2:
863 elf_add_reloc(s, segment, R_X86_64_16);
864 break;
865 case 4:
866 elf_add_reloc(s, segment, R_X86_64_32);
867 break;
868 case 8:
869 elf_add_reloc(s, segment, R_X86_64_64);
870 break;
871 default:
872 error(ERR_PANIC, "internal error elf64-hpa-871");
873 break;
875 } else if (wrt == elf_gotpc_sect + 1) {
877 * The user will supply GOT relative to $$. ELF
878 * will let us have GOT relative to $. So we
879 * need to fix up the data item by $-$$.
881 addr += s->len;
882 elf_add_reloc(s, segment, R_X86_64_GOTPCREL);
883 } else if (wrt == elf_gotoff_sect + 1) {
884 elf_add_reloc(s, segment, R_X86_64_GOTTPOFF);
885 } else if (wrt == elf_got_sect + 1) {
886 addr = elf_add_gsym_reloc(s, segment, addr,
887 R_X86_64_GOT32, TRUE);
888 } else if (wrt == elf_sym_sect + 1) {
889 switch (realbytes) {
890 case 2:
891 gnu16 = 1;
892 addr = elf_add_gsym_reloc(s, segment, addr,
893 R_X86_64_16, FALSE);
894 break;
895 case 4:
896 addr = elf_add_gsym_reloc(s, segment, addr,
897 R_X86_64_32, FALSE);
898 break;
899 case 8:
900 addr = elf_add_gsym_reloc(s, segment, addr,
901 R_X86_64_64, FALSE);
902 break;
903 default:
904 error(ERR_PANIC, "internal error elf64-hpa-903");
905 break;
907 } else if (wrt == elf_plt_sect + 1) {
908 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
909 "relative PLT references");
910 } else {
911 error(ERR_NONFATAL, "ELF format does not support this"
912 " use of WRT");
913 wrt = NO_SEG; /* we can at least _try_ to continue */
917 p = mydata;
918 if (gnu16) {
919 WRITESHORT(p, addr);
920 } else {
921 if (realbytes != 8 && realbytes != 4 && segment != NO_SEG) {
922 error(ERR_NONFATAL,
923 "Unsupported non-64-bit ELF relocation");
925 if (realbytes == 4) WRITELONG(p, addr);
926 else WRITEDLONG(p, (int64_t)addr);
928 elf_sect_write(s, mydata, realbytes);
929 } else if (type == OUT_REL2ADR) {
930 if (segment == segto)
931 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
932 if (segment != NO_SEG && segment % 2) {
933 error(ERR_NONFATAL, "ELF format does not support"
934 " segment base references");
935 } else {
936 if (wrt == NO_SEG) {
937 elf_add_reloc(s, segment, R_X86_64_PC16);
938 } else {
939 error(ERR_NONFATAL,
940 "Unsupported non-32-bit ELF relocation [2]");
943 p = mydata;
944 WRITESHORT(p, *(int32_t *)data - realbytes);
945 elf_sect_write(s, mydata, 2L);
946 } else if (type == OUT_REL4ADR) {
947 if (segment == segto)
948 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
949 if (segment != NO_SEG && segment % 2) {
950 error(ERR_NONFATAL, "ELF format does not support"
951 " segment base references");
952 } else {
953 if (wrt == NO_SEG) {
954 elf_add_reloc(s, segment, R_X86_64_PC32);
955 } else if (wrt == elf_plt_sect + 1) {
956 elf_add_reloc(s, segment, R_X86_64_PLT32);
957 } else if (wrt == elf_gotpc_sect + 1 ||
958 wrt == elf_gotoff_sect + 1 ||
959 wrt == elf_got_sect + 1) {
960 error(ERR_NONFATAL, "ELF format cannot produce PC-"
961 "relative GOT references");
962 } else {
963 error(ERR_NONFATAL, "ELF format does not support this"
964 " use of WRT");
965 wrt = NO_SEG; /* we can at least _try_ to continue */
968 p = mydata;
969 WRITELONG(p, *(int32_t *)data - realbytes);
970 elf_sect_write(s, mydata, 4L);
974 static void elf_write(void)
976 int nsections, align;
977 int scount;
978 char *p;
979 int commlen;
980 char comment[64];
981 int i;
983 struct SAA *symtab;
984 int32_t symtablen, symtablocal;
987 * Work out how many sections we will have. We have SHN_UNDEF,
988 * then the flexible user sections, then the four fixed
989 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
990 * then optionally relocation sections for the user sections.
992 if (of_elf64.current_dfmt == &df_stabs)
993 nsections = 8;
994 else
995 nsections = 5; /* SHN_UNDEF and the fixed ones */
997 add_sectname("", ".comment");
998 add_sectname("", ".shstrtab");
999 add_sectname("", ".symtab");
1000 add_sectname("", ".strtab");
1001 for (i = 0; i < nsects; i++) {
1002 nsections++; /* for the section itself */
1003 if (sects[i]->head) {
1004 nsections++; /* for its relocations without addends*/
1005 add_sectname(".rela", sects[i]->name);
1009 if (of_elf64.current_dfmt == &df_stabs) {
1010 /* in case the debug information is wanted, just add these three sections... */
1011 add_sectname("", ".stab");
1012 add_sectname("", ".stabstr");
1013 add_sectname(".rel", ".stab");
1017 * Do the comment.
1019 *comment = '\0';
1020 commlen =
1021 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1024 * Output the ELF header.
1026 fwrite("\177ELF\2\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
1027 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1028 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1029 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1030 fwriteint64_t(0L, elffp); /* no entry point */
1031 fwriteint64_t(0L, elffp); /* no program header table */
1032 fwriteint64_t(0x40L, elffp); /* section headers straight after
1033 * ELF header plus alignment */
1034 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1035 fwriteint16_t(0x40, elffp); /* size of ELF header */
1036 fwriteint16_t(0, elffp); /* no program header table, again */
1037 fwriteint16_t(0, elffp); /* still no program header table */
1038 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1039 fwriteint16_t(nsections, elffp); /* number of sections */
1040 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1041 * section header table */
1044 * Build the symbol table and relocation tables.
1046 symtab = elf_build_symtab(&symtablen, &symtablocal);
1047 for (i = 0; i < nsects; i++)
1048 if (sects[i]->head)
1049 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1050 sects[i]->head);
1053 * Now output the section header table.
1056 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1057 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1058 elf_foffs += align;
1059 elf_nsect = 0;
1060 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1062 elf_section_header(0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1063 scount = 1; /* needed for the stabs debugging to track the symtable section */
1064 p = shstrtab + 1;
1065 for (i = 0; i < nsects; i++) {
1066 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1067 (sects[i]->type == SHT_PROGBITS ?
1068 sects[i]->data : NULL), TRUE,
1069 sects[i]->len, 0, 0, sects[i]->align, 0);
1070 p += strlen(p) + 1;
1071 scount++; /* dito */
1073 elf_section_header(p - shstrtab, 1, 0, comment, FALSE, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1074 scount++; /* dito */
1075 p += strlen(p) + 1;
1076 elf_section_header(p - shstrtab, 3, 0, shstrtab, FALSE, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1077 scount++; /* dito */
1078 p += strlen(p) + 1;
1079 elf_section_header(p - shstrtab, 2, 0, symtab, TRUE, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1080 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1081 p += strlen(p) + 1;
1082 elf_section_header(p - shstrtab, 3, 0, strs, TRUE, strslen, 0, 0, 1, 0); /* .strtab */
1083 for (i = 0; i < nsects; i++)
1084 if (sects[i]->head) {
1085 p += strlen(p) + 1;
1086 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, TRUE,
1087 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1089 if (of_elf64.current_dfmt == &df_stabs) {
1090 /* for debugging information, create the last three sections
1091 which are the .stab , .stabstr and .rel.stab sections respectively */
1093 /* this function call creates the stab sections in memory */
1094 stabs64_generate();
1096 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1097 p += strlen(p) + 1;
1098 elf_section_header(p - shstrtab, 1, 0, stabbuf, 0, stablen,
1099 nsections - 2, 0, 4, 12);
1101 p += strlen(p) + 1;
1102 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, 0,
1103 stabstrlen, 0, 0, 4, 0);
1105 p += strlen(p) + 1;
1106 /* link -> symtable info -> section to refer to */
1107 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, 0,
1108 stabrellen, symtabsection, nsections - 3, 4,
1109 16);
1112 fwrite(align_str, align, 1, elffp);
1115 * Now output the sections.
1117 elf_write_sections();
1119 nasm_free(elf_sects);
1120 saa_free(symtab);
1123 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1125 struct SAA *s = saa_init(1L);
1126 struct Symbol *sym;
1127 uint8_t entry[24], *p;
1128 int i;
1130 *len = *local = 0;
1133 * First, an all-zeros entry, required by the ELF spec.
1135 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1136 *len += 24;
1137 (*local)++;
1140 * Next, an entry for the file name.
1142 p = entry;
1143 WRITELONG(p, 1); /* we know it's 1st thing in strtab */
1144 WRITESHORT(p, 4); /* type FILE */
1145 WRITESHORT(p, SHN_ABS);
1146 WRITEDLONG(p, (uint64_t) 0); /* no value */
1147 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1148 saa_wbytes(s, entry, 24L);
1149 *len += 24;
1150 (*local)++;
1153 * Now some standard symbols defining the segments, for relocation
1154 * purposes.
1156 for (i = 1; i <= nsects + 1; i++) {
1157 p = entry;
1158 WRITELONG(p, 0); /* no symbol name */
1159 WRITESHORT(p, 3); /* local section-type thing */
1160 WRITESHORT(p, (i == 1 ? SHN_ABS : i - 1)); /* the section id */
1161 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1162 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1163 saa_wbytes(s, entry, 24L);
1164 *len += 24;
1165 (*local)++;
1169 * Now the other local symbols.
1171 saa_rewind(syms);
1172 while ((sym = saa_rstruct(syms))) {
1173 if (sym->type & SYM_GLOBAL)
1174 continue;
1175 p = entry;
1176 WRITELONG(p, sym->strpos);
1177 WRITECHAR(p, sym->type); /* local non-typed thing */
1178 WRITECHAR(p, sym->other);
1179 WRITESHORT(p, sym->section);
1180 WRITEDLONG(p, (int64_t)sym->value);
1181 WRITEDLONG(p, (int64_t)sym->size);
1182 saa_wbytes(s, entry, 24L);
1183 *len += 24;
1184 (*local)++;
1188 * Now the global symbols.
1190 saa_rewind(syms);
1191 while ((sym = saa_rstruct(syms))) {
1192 if (!(sym->type & SYM_GLOBAL))
1193 continue;
1194 p = entry;
1195 WRITELONG(p, sym->strpos);
1196 WRITECHAR(p, sym->type); /* global non-typed thing */
1197 WRITECHAR(p, sym->other);
1198 WRITESHORT(p, sym->section);
1199 WRITEDLONG(p, (int64_t)sym->value);
1200 WRITEDLONG(p, (int64_t)sym->size);
1201 saa_wbytes(s, entry, 24L);
1202 *len += 24;
1205 return s;
1208 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1210 struct SAA *s;
1211 uint8_t *p, entry[24];
1213 if (!r)
1214 return NULL;
1216 s = saa_init(1L);
1217 *len = 0;
1219 while (r) {
1220 int64_t sym = r->symbol;
1222 if (sym >= GLOBAL_TEMP_BASE)
1223 sym += -GLOBAL_TEMP_BASE + (nsects + 3) + nlocals;
1225 p = entry;
1226 WRITEDLONG(p, r->address);
1227 WRITEDLONG(p, (sym << 32) + r->type);
1228 WRITEDLONG(p, (uint64_t) 0);
1229 saa_wbytes(s, entry, 24L);
1230 *len += 24;
1232 r = r->next;
1235 return s;
1238 static void elf_section_header(int name, int type, int flags,
1239 void *data, int is_saa, int32_t datalen,
1240 int link, int info, int align, int eltsize)
1242 elf_sects[elf_nsect].data = data;
1243 elf_sects[elf_nsect].len = datalen;
1244 elf_sects[elf_nsect].is_saa = is_saa;
1245 elf_nsect++;
1247 fwriteint32_t((int32_t)name, elffp);
1248 fwriteint32_t((int32_t)type, elffp);
1249 fwriteint64_t((int64_t)flags, elffp);
1250 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1251 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1252 fwriteint64_t(datalen, elffp);
1253 if (data)
1254 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1255 fwriteint32_t((int32_t)link, elffp);
1256 fwriteint32_t((int32_t)info, elffp);
1257 fwriteint64_t((int64_t)align, elffp);
1258 fwriteint64_t((int64_t)eltsize, elffp);
1261 static void elf_write_sections(void)
1263 int i;
1264 for (i = 0; i < elf_nsect; i++)
1265 if (elf_sects[i].data) {
1266 int32_t len = elf_sects[i].len;
1267 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1268 int32_t align = reallen - len;
1269 if (elf_sects[i].is_saa)
1270 saa_fpwrite(elf_sects[i].data, elffp);
1271 else
1272 fwrite(elf_sects[i].data, len, 1, elffp);
1273 fwrite(align_str, align, 1, elffp);
1277 static void elf_sect_write(struct Section *sect,
1278 const uint8_t *data, uint32_t len)
1280 saa_wbytes(sect->data, data, len);
1281 sect->len += len;
1284 static int32_t elf_segbase(int32_t segment)
1286 return segment;
1289 static int elf_directive(char *directive, char *value, int pass)
1291 (void)directive;
1292 (void)value;
1293 (void)pass;
1294 return 0;
1297 static void elf_filename(char *inname, char *outname, efunc error)
1299 strcpy(elf_module, inname);
1300 standard_extension(inname, outname, ".o", error);
1303 static const char *elf_stdmac[] = {
1304 "%define __SECT__ [section .text]",
1305 "%macro __NASM_CDecl__ 1",
1306 "%define $_%1 $%1",
1307 "%endmacro",
1308 NULL
1310 static int elf_set_info(enum geninfo type, char **val)
1312 (void)type;
1313 (void)val;
1314 return 0;
1317 static struct dfmt df_stabs = {
1318 "ELF64 (X86_64) stabs debug format for Linux",
1319 "stabs",
1320 stabs64_init,
1321 stabs64_linenum,
1322 stabs64_deflabel,
1323 stabs64_directive,
1324 stabs64_typevalue,
1325 stabs64_output,
1326 stabs64_cleanup
1329 struct dfmt *elf64_debugs_arr[2] = { &df_stabs, NULL };
1331 struct ofmt of_elf64 = {
1332 "ELF64 (x86_64) object files (e.g. Linux)",
1333 "elf64",
1334 NULL,
1335 elf64_debugs_arr,
1336 &null_debug_form,
1337 elf_stdmac,
1338 elf_init,
1339 elf_set_info,
1340 elf_out,
1341 elf_deflabel,
1342 elf_section_names,
1343 elf_segbase,
1344 elf_directive,
1345 elf_filename,
1346 elf_cleanup
1349 /* again, the stabs debugging stuff (code) */
1351 void stabs64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1353 (void)of;
1354 (void)id;
1355 (void)fp;
1356 (void)error;
1359 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1361 (void)segto;
1363 if (!stabs_filename) {
1364 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1365 strcpy(stabs_filename, filename);
1366 } else {
1367 if (strcmp(stabs_filename, filename)) {
1368 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1369 in fact, this leak comes in quite handy to maintain a list of files
1370 encountered so far in the symbol lines... */
1372 /* why not nasm_free(stabs_filename); we're done with the old one */
1374 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1375 strcpy(stabs_filename, filename);
1378 stabs_immcall = 1;
1379 currentline = linenumber;
1382 void stabs64_deflabel(char *name, int32_t segment, int32_t offset, int is_global,
1383 char *special)
1385 (void)name;
1386 (void)segment;
1387 (void)offset;
1388 (void)is_global;
1389 (void)special;
1392 void stabs64_directive(const char *directive, const char *params)
1394 (void)directive;
1395 (void)params;
1398 void stabs64_typevalue(int32_t type)
1400 (void)type;
1403 void stabs64_output(int type, void *param)
1405 struct symlininfo *s;
1406 struct linelist *el;
1407 if (type == TY_STABSSYMLIN) {
1408 if (stabs_immcall) {
1409 s = (struct symlininfo *)param;
1410 if (strcmp(s->name, ".text"))
1411 return; /* we are only interested in the text stuff */
1412 numlinestabs++;
1413 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1414 el->info.offset = s->offset;
1415 el->info.section = s->section;
1416 el->info.name = s->name;
1417 el->line = currentline;
1418 el->filename = stabs_filename;
1419 el->next = 0;
1420 if (stabslines) {
1421 stabslines->last->next = el;
1422 stabslines->last = el;
1423 } else {
1424 stabslines = el;
1425 stabslines->last = el;
1429 stabs_immcall = 0;
1432 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1433 do {\
1434 WRITELONG(p,n_strx); \
1435 WRITECHAR(p,n_type); \
1436 WRITECHAR(p,n_other); \
1437 WRITESHORT(p,n_desc); \
1438 WRITELONG(p,n_value); \
1439 } while (0)
1441 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1443 void stabs64_generate(void)
1445 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1446 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1447 char **allfiles;
1448 int *fileidx;
1450 struct linelist *ptr;
1452 ptr = stabslines;
1454 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1455 for (i = 0; i < numlinestabs; i++)
1456 allfiles[i] = 0;
1457 numfiles = 0;
1458 while (ptr) {
1459 if (numfiles == 0) {
1460 allfiles[0] = ptr->filename;
1461 numfiles++;
1462 } else {
1463 for (i = 0; i < numfiles; i++) {
1464 if (!strcmp(allfiles[i], ptr->filename))
1465 break;
1467 if (i >= numfiles) {
1468 allfiles[i] = ptr->filename;
1469 numfiles++;
1472 ptr = ptr->next;
1474 strsize = 1;
1475 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1476 for (i = 0; i < numfiles; i++) {
1477 fileidx[i] = strsize;
1478 strsize += strlen(allfiles[i]) + 1;
1480 mainfileindex = 0;
1481 for (i = 0; i < numfiles; i++) {
1482 if (!strcmp(allfiles[i], elf_module)) {
1483 mainfileindex = i;
1484 break;
1488 /* worst case size of the stab buffer would be:
1489 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1491 sbuf =
1492 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1493 sizeof(struct stabentry));
1495 ssbuf = (uint8_t *)nasm_malloc(strsize);
1497 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1498 rptr = rbuf;
1500 for (i = 0; i < numfiles; i++) {
1501 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1503 ssbuf[0] = 0;
1505 stabstrlen = strsize; /* set global variable for length of stab strings */
1507 sptr = sbuf;
1508 /* this is the first stab, its strx points to the filename of the
1509 the source-file, the n_desc field should be set to the number
1510 of remaining stabs
1512 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1514 ptr = stabslines;
1515 numstabs = 0;
1517 if (ptr) {
1518 /* this is the stab for the main source file */
1519 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1521 /* relocation table entry */
1523 /* Since the above WRITE_STAB calls have already */
1524 /* created two entries, the index in the info.section */
1525 /* member must be adjusted by adding 3 */
1527 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1528 WRITEDLONG(rptr, ((int64_t)(ptr->info.section + 3) << 32) | R_X86_64_32);
1530 numstabs++;
1531 currfile = mainfileindex;
1534 while (ptr) {
1535 if (strcmp(allfiles[currfile], ptr->filename)) {
1536 /* oops file has changed... */
1537 for (i = 0; i < numfiles; i++)
1538 if (!strcmp(allfiles[i], ptr->filename))
1539 break;
1540 currfile = i;
1541 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1542 ptr->info.offset);
1543 numstabs++;
1545 /* relocation table entry */
1547 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1548 WRITEDLONG(rptr, ((int64_t)(ptr->info.section + 3) << 32) | R_X86_64_32);
1551 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1552 numstabs++;
1554 /* relocation table entry */
1556 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1557 WRITEDLONG(rptr, ((int64_t)(ptr->info.section + 3) << 32) | R_X86_64_32);
1559 ptr = ptr->next;
1563 ((struct stabentry *)sbuf)->n_desc = numstabs;
1565 nasm_free(allfiles);
1566 nasm_free(fileidx);
1568 stablen = (sptr - sbuf);
1569 stabrellen = (rptr - rbuf);
1570 stabrelbuf = rbuf;
1571 stabbuf = sbuf;
1572 stabstrbuf = ssbuf;
1575 void stabs64_cleanup(void)
1577 struct linelist *ptr, *del;
1578 if (!stabslines)
1579 return;
1580 ptr = stabslines;
1581 while (ptr) {
1582 del = ptr;
1583 ptr = ptr->next;
1584 nasm_free(del);
1586 if (stabbuf)
1587 nasm_free(stabbuf);
1588 if (stabrelbuf)
1589 nasm_free(stabrelbuf);
1590 if (stabstrbuf)
1591 nasm_free(stabstrbuf);
1594 #endif /* OF_ELF */