ELF: add header files, begin merging common code, drop .comment
authorH. Peter Anvin <hpa@zytor.com>
Thu, 25 Jun 2009 19:30:50 +0000 (12:30 -0700)
committerH. Peter Anvin <hpa@zytor.com>
Thu, 25 Jun 2009 19:30:50 +0000 (12:30 -0700)
Add something approaching real ELF header files.

Begin merging the common ELF code, beginning with the section name
detection.

Drop automatic generation of .comment section, and in particular the
treatment of .common as a special section (if we decide generating
.comment is still a good idea, we should just do it as a macro.)

Augment the list of known sections, and make it table-driven.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Makefile.in
doc/changes.src
doc/nasmdoc.src
output/dwarf.h [new file with mode: 0644]
output/elf32.h [new file with mode: 0644]
output/elf64.h [new file with mode: 0644]
output/elfcommon.h [new file with mode: 0644]
output/outelf.c [new file with mode: 0644]
output/outelf.h [new file with mode: 0644]
output/outelf32.c
output/outelf64.c

index 719d375..50736b9 100644 (file)
@@ -65,7 +65,7 @@ NASM =        nasm.$(O) nasmlib.$(O) ver.$(O) \
        assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \
        outform.$(O) outlib.$(O) output/outbin.$(O) \
        output/outaout.$(O) output/outcoff.$(O) \
-       output/outelf32.$(O) output/outelf64.$(O) \
+       output/outelf.$(O) output/outelf32.$(O) output/outelf64.$(O) \
        output/outobj.$(O) output/outas86.$(O) output/outrdf2.$(O) \
        output/outdbg.$(O) output/outieee.$(O) output/outmacho.$(O) \
        preproc.$(O) quote.$(O) pptok.$(O) macros.$(O) \
index 7835d38..60a98e9 100644 (file)
@@ -44,6 +44,8 @@ since 2007.
 \b Removed AMD SSE5, replaced with the new XOP/FMA4/CVT16 (rev 3.03)
    spec.
 
+\b The ELF backends no longer automatically generate a \c{.comment} section.
+
 
 \S{cl-2.05.01} Version 2.05.01
 
index d400469..888a19f 100644 (file)
@@ -5353,15 +5353,20 @@ thread local variables.
 The defaults assumed by NASM if you do not specify the above
 qualifiers are:
 
-\I\c{.text} \I\c{.data} \I\c{.bss} \I\c{.rodata} \I\c{.tdata} \I\c{.tbss}
-
-\c section .text    progbits  alloc  exec    nowrite  align=16
-\c section .rodata  progbits  alloc  noexec  nowrite  align=4
-\c section .data    progbits  alloc  noexec  write    align=4
-\c section .bss     nobits    alloc  noexec  write    align=4
-\c section .tdata   progbits  alloc  noexec  write    align=4    tls
-\c section .tbss    nobits    alloc  noexec  write    align=4    tls
-\c section other    progbits  alloc  noexec  nowrite  align=1
+\I\c{.text} \I\c{.rodata} \I\c{.lrodata} \I\c{.data} \I\c{.ldata}
+\I\c{.bss} \I\c{.lbss} \I\c{.tdata} \I\c{.tbss} \I\c\{.comment}
+
+\c section .text    progbits  alloc   exec    nowrite  align=16
+\c section .rodata  progbits  alloc   noexec  nowrite  align=4
+\c section .lrodata progbits  alloc   noexec  nowrite  align=4
+\c section .data    progbits  alloc   noexec  write    align=4
+\c section .ldata   progbits  alloc   noexec  write    align=4
+\c section .bss     nobits    alloc   noexec  write    align=4
+\c section .lbss    nobits    alloc   noexec  write    align=4
+\c section .tdata   progbits  alloc   noexec  write    align=4    tls
+\c section .tbss    nobits    alloc   noexec  write    align=4    tls
+\c section .comment progbits  noalloc noexec  nowrite  align=1
+\c section other    progbits  alloc   noexec  nowrite  align=1
 
 (Any section name other than those in the above table
  is treated by default like \c{other} in the above table.
diff --git a/output/dwarf.h b/output/dwarf.h
new file mode 100644 (file)
index 0000000..18ce26e
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef OUTPUT_DWARF_H
+#define OUTPUT_DWARF_H
+
+#define    DW_TAG_compile_unit 0x11
+#define    DW_TAG_subprogram    0x2e
+#define    DW_AT_name           0x03
+#define    DW_AT_stmt_list      0x10
+#define    DW_AT_low_pc         0x11
+#define    DW_AT_high_pc        0x12
+#define    DW_AT_language       0x13
+#define    DW_AT_producer       0x25
+#define    DW_AT_frame_base     0x40
+#define    DW_FORM_addr         0x01
+#define    DW_FORM_data2        0x05
+#define    DW_FORM_data4        0x06
+#define    DW_FORM_string       0x08
+#define    DW_LNS_extended_op   0
+#define    DW_LNS_advance_pc    2
+#define    DW_LNS_advance_line  3
+#define    DW_LNS_set_file      4
+#define    DW_LNE_end_sequence  1
+#define    DW_LNE_set_address   2
+#define    DW_LNE_define_file   3
+#define    DW_LANG_Mips_Assembler  0x8001
+
+#endif /* OUTPUT_DWARF_H */
diff --git a/output/elf32.h b/output/elf32.h
new file mode 100644 (file)
index 0000000..ff5e863
--- /dev/null
@@ -0,0 +1,134 @@
+#ifndef OUTPUT_ELF32_H
+#define OUTPUT_ELF32_H
+
+#include "elfcommon.h"
+
+/* ELF standard typedefs (yet more proof that <stdint.h> was way overdue) */
+typedef uint16_t Elf32_Half;
+typedef int16_t Elf32_SHalf;
+typedef uint32_t Elf32_Word;
+typedef int32_t Elf32_Sword;
+typedef uint64_t Elf32_Xword;
+typedef int64_t Elf32_Sxword;
+
+typedef uint32_t Elf32_Off;
+typedef uint32_t Elf32_Addr;
+typedef uint16_t Elf32_Section;
+
+/* Dynamic header */
+
+typedef struct elf32_dyn {
+    Elf32_Sword d_tag;
+    union {
+       Elf32_Sword d_val;
+       Elf32_Addr d_ptr;
+    } d_un;
+} Elf32_Dyn;
+
+/* Relocations */
+
+#define ELF32_R_SYM(x) ((x) >> 8)
+#define ELF32_R_TYPE(x)        ((x) & 0xff)
+
+typedef struct elf32_rel {
+    Elf32_Addr r_offset;
+    Elf32_Word r_info;
+} Elf32_Rel;
+
+typedef struct elf32_rela {
+    Elf32_Addr r_offset;
+    Elf32_Word r_info;
+    Elf32_Sword r_addend;
+} Elf32_Rela;
+
+enum reloc32_type {
+    R_386_32           =  1,   /* ordinary absolute relocation */
+    R_386_PC32          =  2,   /* PC-relative relocation */
+    R_386_GOT32         =  3,   /* an offset into GOT */
+    R_386_PLT32         =  4,   /* a PC-relative offset into PLT */
+    R_386_COPY          =  5,   /* ??? */
+    R_386_GLOB_DAT      =  6,   /* ??? */
+    R_386_JUMP_SLOT     =  7,   /* ??? */
+    R_386_RELATIVE      =  8,   /* ??? */
+    R_386_GOTOFF        =  9,   /* an offset from GOT base */
+    R_386_GOTPC         = 10,   /* a PC-relative offset _to_ GOT */
+    R_386_TLS_TPOFF     = 14,   /* Offset in static TLS block */
+    R_386_TLS_IE        = 15,   /* Address of GOT entry for static TLS
+                                   block offset */
+    /* These are GNU extensions, but useful */
+    R_386_16            = 20,   /* A 16-bit absolute relocation */
+    R_386_PC16          = 21,   /* A 16-bit PC-relative relocation */
+    R_386_8             = 22,   /* An 8-bit absolute relocation */
+    R_386_PC8           = 23    /* An 8-bit PC-relative relocation */
+};
+
+/* Symbol */
+
+typedef struct elf32_sym {
+    Elf32_Word st_name;
+    Elf32_Addr st_value;
+    Elf32_Word st_size;
+    unsigned char st_info;
+    unsigned char st_other;
+    Elf32_Half st_shndx;
+} Elf32_Sym;
+
+/* Main file header */
+
+typedef struct elf32_hdr {
+    unsigned char e_ident[EI_NIDENT];
+    Elf32_Half e_type;
+    Elf32_Half e_machine;
+    Elf32_Word e_version;
+    Elf32_Addr e_entry;
+    Elf32_Off e_phoff;
+    Elf32_Off e_shoff;
+    Elf32_Word e_flags;
+    Elf32_Half e_ehsize;
+    Elf32_Half e_phentsize;
+    Elf32_Half e_phnum;
+    Elf32_Half e_shentsize;
+    Elf32_Half e_shnum;
+    Elf32_Half e_shstrndx;
+} Elf32_Ehdr;
+
+/* Program header */
+
+typedef struct elf32_phdr {
+    Elf32_Word p_type;
+    Elf32_Off p_offset;
+    Elf32_Addr p_vaddr;
+    Elf32_Addr p_paddr;
+    Elf32_Word p_filesz;
+    Elf32_Word p_memsz;
+    Elf32_Word p_flags;
+    Elf32_Word p_align;
+} Elf32_Phdr;
+
+/* Section header */
+
+typedef struct elf32_shdr {
+    Elf32_Word sh_name;
+    Elf32_Word sh_type;
+    Elf32_Word sh_flags;
+    Elf32_Addr sh_addr;
+    Elf32_Off sh_offset;
+    Elf32_Word sh_size;
+    Elf32_Word sh_link;
+    Elf32_Word sh_info;
+    Elf32_Word sh_addralign;
+    Elf32_Word sh_entsize;
+} Elf32_Shdr;
+
+/* Note header */
+typedef struct elf32_note {
+    Elf32_Word n_namesz;       /* Name size */
+    Elf32_Word n_descsz;       /* Content size */
+    Elf32_Word n_type; /* Content type */
+} Elf32_Nhdr;
+
+/* How to extract and insert information held in the st_info field.  */
+#define ELF32_ST_BIND(val)             (((unsigned char) (val)) >> 4)
+#define ELF32_ST_TYPE(val)             ((val) & 0xf)
+
+#endif /* OUTPUT_ELF32_H */
diff --git a/output/elf64.h b/output/elf64.h
new file mode 100644 (file)
index 0000000..626a24e
--- /dev/null
@@ -0,0 +1,156 @@
+#ifndef OUTPUT_ELF64_H
+#define OUTPUT_ELF64_H
+
+#include "elfcommon.h"
+
+/* ELF standard typedefs (yet more proof that <stdint.h> was way overdue) */
+typedef uint16_t Elf64_Half;
+typedef int16_t Elf64_SHalf;
+typedef uint32_t Elf64_Word;
+typedef int32_t Elf64_Sword;
+typedef uint64_t Elf64_Xword;
+typedef int64_t Elf64_Sxword;
+
+typedef uint64_t Elf64_Off;
+typedef uint64_t Elf64_Addr;
+typedef uint16_t Elf64_Section;
+
+/* Dynamic header */
+
+typedef struct elf64_dyn {
+    Elf64_Sxword d_tag;
+    union {
+       Elf64_Xword d_val;
+       Elf64_Addr d_ptr;
+    } d_un;
+} Elf64_Dyn;
+
+/* Relocations */
+
+#define ELF64_R_SYM(x) ((x) >> 32)
+#define ELF64_R_TYPE(x)        ((x) & 0xffffffff)
+
+typedef struct elf64_rel {
+    Elf64_Addr r_offset;
+    Elf64_Xword r_info;
+} Elf64_Rel;
+
+typedef struct elf64_rela {
+    Elf64_Addr r_offset;
+    Elf64_Xword r_info;
+    Elf64_Sxword r_addend;
+} Elf64_Rela;
+
+enum reloc64_type {
+    R_X86_64_NONE       =  0,  /* No reloc */
+    R_X86_64_64                =  1,   /* Direct 64 bit  */
+    R_X86_64_PC32      =  2,   /* PC relative 32 bit signed */
+    R_X86_64_GOT32     =  3,   /* 32 bit GOT entry */
+    R_X86_64_PLT32     =  4,   /* 32 bit PLT address */
+    R_X86_64_COPY      =  5,   /* Copy symbol at runtime */
+    R_X86_64_GLOB_DAT  =  6,   /* Create GOT entry */
+    R_X86_64_JUMP_SLOT =  7,   /* Create PLT entry */
+    R_X86_64_RELATIVE  =  8,   /* Adjust by program base */
+    R_X86_64_GOTPCREL  =  9,   /* 32 bit signed PC relative offset to GOT */
+    R_X86_64_32                = 10,   /* Direct 32 bit zero extended */
+    R_X86_64_32S       = 11,   /* Direct 32 bit sign extended */
+    R_X86_64_16                = 12,   /* Direct 16 bit zero extended */
+    R_X86_64_PC16      = 13,   /* 16 bit sign extended pc relative */
+    R_X86_64_8         = 14,   /* Direct 8 bit sign extended  */
+    R_X86_64_PC8       = 15,   /* 8 bit sign extended pc relative */
+    R_X86_64_DTPMOD64  = 16,   /* ID of module containing symbol */
+    R_X86_64_DTPOFF64  = 17,   /* Offset in module's TLS block */
+    R_X86_64_TPOFF64   = 18,   /* Offset in initial TLS block */
+    R_X86_64_TLSGD     = 19,   /* 32 bit signed PC relative offset
+                                  to two GOT entries for GD symbol */
+    R_X86_64_TLSLD     = 20,   /* 32 bit signed PC relative offset
+                                  to two GOT entries for LD symbol */
+    R_X86_64_DTPOFF32  = 21,   /* Offset in TLS block */
+    R_X86_64_GOTTPOFF  = 22,   /* 32 bit signed PC relative offset
+                                  to GOT entry for IE symbol */
+    R_X86_64_TPOFF32   = 23,   /* Offset in initial TLS block */
+    R_X86_64_PC64      = 24,   /* word64 S + A - P */
+    R_X86_64_GOTOFF64  = 25,   /* word64 S + A - GOT */
+    R_X86_64_GOTPC32   = 26,   /* word32 GOT + A - P */
+    R_X86_64_GOT64     = 27,   /* word64 G + A */
+    R_X86_64_GOTPCREL64        = 28,   /* word64 G + GOT - P + A */
+    R_X86_64_GOTPC64   = 29,   /* word64 GOT - P + A */
+    R_X86_64_GOTPLT64  = 30,   /* word64 G + A */
+    R_X86_64_PLTOFF64  = 31,   /* word64 L - GOT + A */
+    R_X86_64_SIZE32    = 32,   /* word32 Z + A */
+    R_X86_64_SIZE64    = 33,   /* word64 Z + A */
+    R_X86_64_GOTPC32_TLSDESC = 34,     /* word32 */
+    R_X86_64_TLSDESC_CALL    = 35,     /* none */
+    R_X86_64_TLSDESC    = 36   /* word64×2 */
+};
+
+/* Symbol */
+
+typedef struct elf64_sym {
+    Elf64_Word st_name;
+    unsigned char st_info;
+    unsigned char st_other;
+    Elf64_Half st_shndx;
+    Elf64_Addr st_value;
+    Elf64_Xword st_size;
+} Elf64_Sym;
+
+/* Main file header */
+
+typedef struct elf64_hdr {
+    unsigned char e_ident[EI_NIDENT];
+    Elf64_Half e_type;
+    Elf64_Half e_machine;
+    Elf64_Word e_version;
+    Elf64_Addr e_entry;
+    Elf64_Off e_phoff;
+    Elf64_Off e_shoff;
+    Elf64_Word e_flags;
+    Elf64_Half e_ehsize;
+    Elf64_Half e_phentsize;
+    Elf64_Half e_phnum;
+    Elf64_Half e_shentsize;
+    Elf64_Half e_shnum;
+    Elf64_Half e_shstrndx;
+} Elf64_Ehdr;
+
+/* Program header */
+
+typedef struct elf64_phdr {
+    Elf64_Word p_type;
+    Elf64_Word p_flags;
+    Elf64_Off p_offset;
+    Elf64_Addr p_vaddr;
+    Elf64_Addr p_paddr;
+    Elf64_Xword p_filesz;
+    Elf64_Xword p_memsz;
+    Elf64_Xword p_align;
+} Elf64_Phdr;
+
+/* Section header */
+
+typedef struct elf64_shdr {
+    Elf64_Word sh_name;
+    Elf64_Word sh_type;
+    Elf64_Xword sh_flags;
+    Elf64_Addr sh_addr;
+    Elf64_Off sh_offset;
+    Elf64_Xword sh_size;
+    Elf64_Word sh_link;
+    Elf64_Word sh_info;
+    Elf64_Xword sh_addralign;
+    Elf64_Xword sh_entsize;
+} Elf64_Shdr;
+
+/* Note header */
+typedef struct elf64_note {
+    Elf64_Word n_namesz;       /* Name size */
+    Elf64_Word n_descsz;       /* Content size */
+    Elf64_Word n_type; /* Content type */
+} Elf64_Nhdr;
+
+/* Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field.  */
+#define ELF64_ST_BIND(val)             ELF32_ST_BIND (val)
+#define ELF64_ST_TYPE(val)             ELF32_ST_TYPE (val)
+
+#endif /* OUTPUT_ELF64_H */
diff --git a/output/elfcommon.h b/output/elfcommon.h
new file mode 100644 (file)
index 0000000..5769980
--- /dev/null
@@ -0,0 +1,217 @@
+#ifndef OUTPUT_ELFCOMMON_H
+#define OUTPUT_ELFCOMMON_H
+
+#include "compiler.h"
+#include <inttypes.h>
+
+/* Segment types */
+#define PT_NULL        0
+#define PT_LOAD        1
+#define PT_DYNAMIC     2
+#define PT_INTERP      3
+#define PT_NOTE        4
+#define PT_SHLIB       5
+#define PT_PHDR        6
+#define PT_LOOS        0x60000000
+#define PT_HIOS        0x6fffffff
+#define PT_LOPROC      0x70000000
+#define PT_HIPROC      0x7fffffff
+#define PT_GNU_EH_FRAME        0x6474e550      /* Extension, eh? */
+
+/* ELF file types */
+#define ET_NONE        0
+#define ET_REL         1
+#define ET_EXEC        2
+#define ET_DYN         3
+#define ET_CORE        4
+#define ET_LOPROC      0xff00
+#define ET_HIPROC      0xffff
+
+/* ELF machine types */
+#define EM_NONE        0
+#define EM_M32         1
+#define EM_SPARC       2
+#define EM_386         3
+#define EM_68K         4
+#define EM_88K         5
+#define EM_486         6       /* Not used in Linux at least */
+#define EM_860         7
+#define EM_MIPS         8      /* R3k, bigendian(?) */
+#define EM_MIPS_RS4_BE         10      /* R4k BE */
+#define EM_PARISC              15
+#define EM_SPARC32PLUS         18
+#define EM_PPC                 20
+#define EM_PPC64               21
+#define EM_S390         22
+#define EM_SH                  42
+#define EM_SPARCV9     43      /* v9 = SPARC64 */
+#define EM_H8_300H      47
+#define EM_H8S          48
+#define EM_IA_64        50
+#define EM_X86_64       62
+#define EM_CRIS         76
+#define EM_V850         87
+#define EM_ALPHA        0x9026 /* Interrim Alpha that stuck around */
+#define EM_CYGNUS_V850  0x9080 /* Old v850 ID used by Cygnus */
+#define EM_S390_OLD     0xA390 /* Obsolete interrim value for S/390 */
+
+/* Dynamic type values */
+#define DT_NULL                0
+#define DT_NEEDED      1
+#define DT_PLTRELSZ    2
+#define DT_PLTGOT      3
+#define DT_HASH                4
+#define DT_STRTAB      5
+#define DT_SYMTAB      6
+#define DT_RELA                7
+#define DT_RELASZ      8
+#define DT_RELAENT     9
+#define DT_STRSZ       10
+#define DT_SYMENT      11
+#define DT_INIT                12
+#define DT_FINI                13
+#define DT_SONAME      14
+#define DT_RPATH       15
+#define DT_SYMBOLIC    16
+#define DT_REL         17
+#define DT_RELSZ       18
+#define DT_RELENT      19
+#define DT_PLTREL      20
+#define DT_DEBUG       21
+#define DT_TEXTREL     22
+#define DT_JMPREL      23
+#define DT_LOPROC      0x70000000
+#define DT_HIPROC      0x7fffffff
+
+/* Auxilliary table entries */
+#define AT_NULL                0       /* end of vector */
+#define AT_IGNORE      1       /* entry should be ignored */
+#define AT_EXECFD      2       /* file descriptor of program */
+#define AT_PHDR                3       /* program headers for program */
+#define AT_PHENT       4       /* size of program header entry */
+#define AT_PHNUM       5       /* number of program headers */
+#define AT_PAGESZ      6       /* system page size */
+#define AT_BASE                7       /* base address of interpreter */
+#define AT_FLAGS       8       /* flags */
+#define AT_ENTRY       9       /* entry point of program */
+#define AT_NOTELF      10      /* program is not ELF */
+#define AT_UID         11      /* real uid */
+#define AT_EUID                12      /* effective uid */
+#define AT_GID         13      /* real gid */
+#define AT_EGID                14      /* effective gid */
+#define AT_PLATFORM    15      /* string identifying CPU for optimizations */
+#define AT_HWCAP       16      /* arch dependent hints at CPU capabilities */
+#define AT_CLKTCK      17      /* frequency at which times() increments */
+/* 18..22 = ? */
+#define AT_SECURE      23      /* secure mode boolean */
+
+/* Program header permission flags */
+#define PF_X            0x1
+#define PF_W            0x2
+#define PF_R            0x4
+
+/* Section header types */
+#define SHT_NULL        0
+#define SHT_PROGBITS    1
+#define SHT_SYMTAB      2
+#define SHT_STRTAB      3
+#define SHT_RELA        4
+#define SHT_HASH        5
+#define SHT_DYNAMIC     6
+#define SHT_NOTE        7
+#define SHT_NOBITS      8
+#define SHT_REL         9
+#define SHT_SHLIB       10
+#define SHT_DYNSYM      11
+#define SHT_NUM         12
+#define SHT_LOPROC      0x70000000
+#define SHT_HIPROC      0x7fffffff
+#define SHT_LOUSER      0x80000000
+#define SHT_HIUSER      0xffffffff
+
+/* Section header flags */
+#define SHF_WRITE            (1 << 0)   /* Writable */
+#define SHF_ALLOC            (1 << 1)   /* Occupies memory during execution */
+#define SHF_EXECINSTR        (1 << 2)   /* Executable */
+#define SHF_MERGE            (1 << 4)   /* Might be merged */
+#define SHF_STRINGS          (1 << 5)   /* Contains nul-terminated strings */
+#define SHF_INFO_LINK        (1 << 6)   /* `sh_info' contains SHT index */
+#define SHF_LINK_ORDER       (1 << 7)   /* Preserve order after combining */
+#define SHF_OS_NONCONFORMING (1 << 8)   /* Non-standard OS specific handling
+                                           required */
+#define SHF_GROUP            (1 << 9)   /* Section is member of a group.  */
+#define SHF_TLS              (1 << 10)  /* Section hold thread-local data.  */
+
+/* Special section numbers */
+#define SHN_UNDEF       0
+#define SHN_LORESERVE   0xff00
+#define SHN_LOPROC      0xff00
+#define SHN_HIPROC      0xff1f
+#define SHN_ABS         0xfff1
+#define SHN_COMMON      0xfff2
+#define SHN_HIRESERVE   0xffff
+
+/* Lenght of magic at the start of a file */
+#define EI_NIDENT      16
+
+/* Magic number constants... */
+#define EI_MAG0         0      /* e_ident[] indexes */
+#define EI_MAG1         1
+#define EI_MAG2         2
+#define EI_MAG3         3
+#define EI_CLASS        4
+#define EI_DATA         5
+#define EI_VERSION      6
+#define EI_OSABI        7
+#define EI_PAD          8
+
+#define ELFMAG0         0x7f   /* EI_MAG */
+#define ELFMAG1         'E'
+#define ELFMAG2         'L'
+#define ELFMAG3         'F'
+#define ELFMAG          "\177ELF"
+#define SELFMAG         4
+
+#define ELFCLASSNONE    0      /* EI_CLASS */
+#define ELFCLASS32      1
+#define ELFCLASS64      2
+#define ELFCLASSNUM     3
+
+#define ELFDATANONE     0      /* e_ident[EI_DATA] */
+#define ELFDATA2LSB     1
+#define ELFDATA2MSB     2
+
+#define EV_NONE         0      /* e_version, EI_VERSION */
+#define EV_CURRENT      1
+#define EV_NUM          2
+
+#define ELFOSABI_NONE   0
+#define ELFOSABI_LINUX  3
+
+/* Legal values for ST_BIND subfield of st_info (symbol binding).  */
+#define STB_LOCAL      0               /* Local symbol */
+#define STB_GLOBAL     1               /* Global symbol */
+#define STB_WEAK       2               /* Weak symbol */
+#define STB_NUM                3               /* Number of defined types.  */
+#define STB_LOOS       10              /* Start of OS-specific */
+#define STB_HIOS       12              /* End of OS-specific */
+#define STB_LOPROC     13              /* Start of processor-specific */
+#define STB_HIPROC     15              /* End of processor-specific */
+
+/* Symbol types */
+#define STT_NOTYPE     0               /* Symbol type is unspecified */
+#define STT_OBJECT     1               /* Symbol is a data object */
+#define STT_FUNC       2               /* Symbol is a code object */
+#define STT_SECTION    3               /* Symbol associated with a section */
+#define STT_FILE       4               /* Symbol's name is file name */
+#define STT_COMMON     5               /* Symbol is a common data object */
+#define STT_TLS                6               /* Symbol is thread-local data object*/
+#define        STT_NUM         7               /* Number of defined types.  */
+
+/* Symbol visibilities */
+#define STV_DEFAULT     0               /* Default symbol visibility rules */
+#define STV_INTERNAL    1               /* Processor specific hidden class */
+#define STV_HIDDEN      2               /* Sym unavailable in other modules */
+#define STV_PROTECTED   3               /* Not preemptible, not exported */
+
+#endif /* OUTPUT_ELFCOMMON_H */
diff --git a/output/outelf.c b/output/outelf.c
new file mode 100644 (file)
index 0000000..69acf3f
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Common code for outelf32 and outelf64
+ */
+
+#include "compiler.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+
+#include "nasm.h"
+
+#include "elfcommon.h"
+#include "dwarf.h"
+#include "outelf.h"
+
+const struct elf_known_section elf_known_sections[] = {
+    { ".text",    SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR,     16 },
+    { ".rodata",  SHT_PROGBITS, SHF_ALLOC,                    4 },
+    { ".lrodata", SHT_PROGBITS, SHF_ALLOC,                    4 },
+    { ".data",    SHT_PROGBITS, SHF_ALLOC|SHF_WRITE,          4 },
+    { ".ldata",   SHT_PROGBITS, SHF_ALLOC|SHF_WRITE,          4 },
+    { ".bss",     SHT_NOBITS,   SHF_ALLOC|SHF_WRITE,          4 },
+    { ".lbss",    SHT_NOBITS,   SHF_ALLOC|SHF_WRITE,          4 },
+    { ".tdata",   SHT_PROGBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS,  4 },
+    { ".tbss",    SHT_NOBITS,   SHF_ALLOC|SHF_WRITE|SHF_TLS,  4 },
+    { ".comment", SHT_PROGBITS, 0,                            1 },
+    { NULL,       SHT_PROGBITS, SHF_ALLOC,                    1 } /* default */
+};
diff --git a/output/outelf.h b/output/outelf.h
new file mode 100644 (file)
index 0000000..15373a3
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Internal definitions common to outelf32 and outelf64
+ */
+#ifndef OUTPUT_OUTELF_H
+#define OUTPUT_OUTELF_H
+
+#define SYM_GLOBAL 0x10
+
+#define GLOBAL_TEMP_BASE 1048576     /* bigger than any reasonable sym id */
+
+#define SEG_ALIGN 16            /* alignment of sections in file */
+#define SEG_ALIGN_1 (SEG_ALIGN-1)
+
+/* this stuff is needed for the stabs debugging format */
+#define N_SO 0x64               /* ID for main source file */
+#define N_SOL 0x84              /* ID for sub-source file */
+#define N_BINCL 0x82
+#define N_EINCL 0xA2
+#define N_SLINE 0x44
+#define TY_STABSSYMLIN 0x40     /* ouch */
+
+/* this stuff is needed for the dwarf debugging format */
+#define TY_DEBUGSYMLIN 0x40     /* internal call to debug_out */
+
+/* Known sections with nonstandard defaults */
+struct elf_known_section {
+    const char *name;          /* Name of section */
+    int type;                  /* Section type (SHT_) */
+    uint32_t flags;            /* Section flags (SHF_) */
+    uint32_t align;            /* Section alignment */
+};
+extern const struct elf_known_section elf_known_sections[];
+
+#endif /* OUTPUT_OUTELF_H */
index 9edce37..f413c23 100644 (file)
 #include "outlib.h"
 #include "rbtree.h"
 
+#include "elf32.h"
+#include "dwarf.h"
+#include "outelf.h"
+
 #ifdef OF_ELF32
 
 /*
  * Relocation types.
  */
-enum reloc_type {
-    R_386_32 = 1,               /* ordinary absolute relocation */
-    R_386_PC32 = 2,             /* PC-relative relocation */
-    R_386_GOT32 = 3,            /* an offset into GOT */
-    R_386_PLT32 = 4,            /* a PC-relative offset into PLT */
-    R_386_COPY = 5,             /* ??? */
-    R_386_GLOB_DAT = 6,         /* ??? */
-    R_386_JUMP_SLOT = 7,        /* ??? */
-    R_386_RELATIVE = 8,         /* ??? */
-    R_386_GOTOFF = 9,           /* an offset from GOT base */
-    R_386_GOTPC = 10,           /* a PC-relative offset _to_ GOT */
-    R_386_TLS_TPOFF = 14,      /* Offset in static TLS block */
-    R_386_TLS_IE = 15,         /* Address of GOT entry for static TLS
-                                          block offset */
-    /* These are GNU extensions, but useful */
-    R_386_16 = 20,              /* A 16-bit absolute relocation */
-    R_386_PC16 = 21,            /* A 16-bit PC-relative relocation */
-    R_386_8 = 22,               /* An 8-bit absolute relocation */
-    R_386_PC8 = 23              /* An 8-bit PC-relative relocation */
-};
-
 struct Reloc {
     struct Reloc *next;
     int32_t address;               /* relative to _start_ of section */
@@ -69,20 +52,12 @@ struct Symbol {
     char *name;                       /* used temporarily if in above list */
 };
 
-#define SHT_PROGBITS 1
-#define SHT_NOBITS 8
-
-#define SHF_WRITE 1
-#define SHF_ALLOC 2
-#define SHF_EXECINSTR 4
-#define SHF_TLS   (1 << 10)    /* Section holds thread-local data.  */
-
 struct Section {
     struct SAA *data;
     uint32_t len, size, nrelocs;
     int32_t index;
     int type;                   /* SHT_PROGBITS or SHT_NOBITS */
-    int align;                  /* alignment: power of two */
+    uint32_t align;             /* alignment: power of two */
     uint32_t flags;            /* section flags */
     char *name;
     struct SAA *rel;
@@ -123,56 +98,6 @@ static uint8_t elf_abiver = 0;      /* Current ABI version */
 extern struct ofmt of_elf32;
 extern struct ofmt of_elf;
 
-#define SHN_ABS 0xFFF1
-#define SHN_COMMON 0xFFF2
-#define SHN_UNDEF 0
-
-#define SYM_GLOBAL 0x10
-
-#define SHT_RELA         4             /* Relocation entries with addends */
-
-#define STT_NOTYPE     0               /* Symbol type is unspecified */
-#define STT_OBJECT     1               /* Symbol is a data object */
-#define STT_FUNC       2               /* Symbol is a code object */
-#define STT_SECTION    3               /* Symbol associated with a section */
-#define STT_FILE       4               /* Symbol's name is file name */
-#define STT_COMMON     5               /* Symbol is a common data object */
-#define STT_TLS                6               /* Symbol is thread-local data object*/
-#define        STT_NUM         7               /* Number of defined types.  */
-
-#define STV_DEFAULT 0
-#define STV_INTERNAL 1
-#define STV_HIDDEN 2
-#define STV_PROTECTED 3
-
-#define GLOBAL_TEMP_BASE 1048576     /* bigger than any reasonable sym id */
-
-#define SEG_ALIGN 16            /* alignment of sections in file */
-#define SEG_ALIGN_1 (SEG_ALIGN-1)
-
-/* Definitions in lieu of dwarf.h */
-#define    DW_TAG_compile_unit   0x11
-#define    DW_TAG_subprogram   0x2e
-#define    DW_AT_name   0x03
-#define    DW_AT_stmt_list   0x10
-#define    DW_AT_low_pc   0x11
-#define    DW_AT_high_pc   0x12
-#define    DW_AT_language  0x13
-#define    DW_AT_producer   0x25
-#define    DW_AT_frame_base   0x40
-#define    DW_FORM_addr   0x01
-#define    DW_FORM_data2   0x05
-#define    DW_FORM_data4   0x06
-#define    DW_FORM_string   0x08
-#define    DW_LNS_extended_op  0
-#define    DW_LNS_advance_pc   2
-#define    DW_LNS_advance_line   3
-#define    DW_LNS_set_file   4
-#define    DW_LNE_end_sequence   1
-#define    DW_LNE_set_address   2
-#define    DW_LNE_define_file   3
-#define    DW_LANG_Mips_Assembler  0x8001
-
 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
 
 static struct ELF_SECTDATA {
@@ -193,14 +118,6 @@ static struct SAA *elf_build_symtab(int32_t *, int32_t *);
 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
 static void add_sectname(char *, char *);
 
-/* this stuff is needed for the stabs debugging format */
-#define N_SO 0x64               /* ID for main source file */
-#define N_SOL 0x84              /* ID for sub-source file */
-#define N_BINCL 0x82
-#define N_EINCL 0xA2
-#define N_SLINE 0x44
-#define TY_STABSSYMLIN 0x40     /* ouch */
-
 struct stabentry {
     uint32_t n_strx;
     uint8_t n_type;
@@ -416,11 +333,13 @@ static int elf_make_section(char *name, int type, int flags, int align)
     return nsects - 1;
 }
 
+
 static int32_t elf_section_names(char *name, int pass, int *bits)
 {
     char *p;
-    unsigned flags_and, flags_or;
-    int type, align, i;
+    uint32_t flags, flags_and, flags_or;
+    uint32_t align;
+    int type, i;
 
     /*
      * Default is 32 bits.
@@ -486,9 +405,9 @@ static int32_t elf_section_names(char *name, int pass, int *bits)
                   " declaration of section `%s'", q, name);
     }
 
-    if (!strcmp(name, ".comment") ||
-        !strcmp(name, ".shstrtab") ||
-        !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
+    if (!strcmp(name, ".shstrtab") ||
+        !strcmp(name, ".symtab") ||
+        !strcmp(name, ".strtab")) {
         error(ERR_NONFATAL, "attempt to redefine reserved section"
               "name `%s'", name);
         return NO_SEG;
@@ -498,34 +417,22 @@ static int32_t elf_section_names(char *name, int pass, int *bits)
         if (!strcmp(name, sects[i]->name))
             break;
     if (i == nsects) {
-        if (!strcmp(name, ".text"))
-            i = elf_make_section(name, SHT_PROGBITS,
-                                 SHF_ALLOC | SHF_EXECINSTR, 16);
-        else if (!strcmp(name, ".rodata"))
-            i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
-        else if (!strcmp(name, ".data"))
-            i = elf_make_section(name, SHT_PROGBITS,
-                                 SHF_ALLOC | SHF_WRITE, 4);
-        else if (!strcmp(name, ".bss"))
-            i = elf_make_section(name, SHT_NOBITS,
-                                 SHF_ALLOC | SHF_WRITE, 4);
-        else if (!strcmp(name, ".tdata"))
-            i = elf_make_section(name, SHT_PROGBITS,
-                                 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
-        else if (!strcmp(name, ".tbss"))
-            i = elf_make_section(name, SHT_NOBITS,
-                                 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
-        else
-            i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
-        if (type)
-            sects[i]->type = type;
-        if (align)
-            sects[i]->align = align;
-        sects[i]->flags &= ~flags_and;
-        sects[i]->flags |= flags_or;
+       const struct elf_known_section *ks = elf_known_sections;
+
+       while (ks->name) {
+           if (!strcmp(name, ks->name))
+               break;
+           ks++;
+       }
+
+       type = type ? type : ks->type;
+       align = align ? align : ks->align;
+       flags = (ks->flags & ~flags_and) | flags_or;
+
+       i = elf_make_section(name, type, flags, align);
     } else if (pass == 1) {
           if ((type && sects[i]->type != type)
-             || (align && sects[i]->align != align)
+             || (align && sects[i]->align != align)
              || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
             error(ERR_WARNING, "section attributes ignored on"
                   " redeclaration of section `%s'", name);
@@ -1035,8 +942,6 @@ static void elf_write(void)
     int align;
     int scount;
     char *p;
-    int commlen;
-    char comment[64];
     int i;
 
     struct SAA *symtab;
@@ -1044,18 +949,16 @@ static void elf_write(void)
 
     /*
      * Work out how many sections we will have. We have SHN_UNDEF,
-     * then the flexible user sections, then the four fixed
-     * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
-     * then optionally relocation sections for the user sections.
+     * then the flexible user sections, then the fixed sections
+     * `.shstrtab', `.symtab' and `.strtab', then optionally
+     * relocation sections for the user sections.
      */
+    nsections = 4;
     if (of_elf32.current_dfmt == &df_stabs)
-        nsections = 8;
+        nsections += 3;
     else if (of_elf32.current_dfmt == &df_dwarf)
-        nsections = 15;
-    else
-        nsections = 5;          /* SHN_UNDEF and the fixed ones */
+        nsections += 10;
 
-    add_sectname("", ".comment");
     add_sectname("", ".shstrtab");
     add_sectname("", ".symtab");
     add_sectname("", ".strtab");
@@ -1095,12 +998,6 @@ static void elf_write(void)
     }
 
     /*
-     * Do the comment.
-     */
-    *comment = '\0';
-    commlen = 2 + snprintf(comment+1, sizeof comment-1, "%s", nasm_comment);
-
-    /*
      * Output the ELF header.
      */
     fwrite("\177ELF\1\1\1", 7, 1, elffp);
@@ -1156,9 +1053,6 @@ static void elf_write(void)
         p += strlen(p) + 1;
         scount++;               /* dito */
     }
-    elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0);  /* .comment */
-    scount++;                   /* dito */
-    p += strlen(p) + 1;
     elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0);     /* .shstrtab */
     scount++;                   /* dito */
     p += strlen(p) + 1;
@@ -1503,7 +1397,7 @@ static int elf_set_info(enum geninfo type, char **val)
     return 0;
 }
 static struct dfmt df_dwarf = {
-    "ELF32 (i386) dwarf debug format for Linux",
+    "ELF32 (i386) dwarf debug format for Linux/Unix",
     "dwarf",
     debug32_init,
     dwarf32_linenum,
@@ -1514,7 +1408,7 @@ static struct dfmt df_dwarf = {
     dwarf32_cleanup
 };
 static struct dfmt df_stabs = {
-    "ELF32 (i386) stabs debug format for Linux",
+    "ELF32 (i386) stabs debug format for Linux/Unix",
     "stabs",
     debug32_init,
     stabs32_linenum,
index 484c4ba..4b2325f 100644 (file)
 #include "outlib.h"
 #include "rbtree.h"
 
-/* Definitions in lieu of elf.h */
-#define SHT_NULL 0                     /* Inactive section header */
-#define SHT_PROGBITS 1                 /* Program defined content */
-#define SHT_RELA         4             /* Relocation entries with addends */
-#define SHT_NOBITS 8                   /* Section requires no space in file */
-#define SHF_WRITE           (1 << 0)   /* Writable */
-#define SHF_ALLOC           (1 << 1)   /* Occupies memory during execution */
-#define SHF_EXECINSTR       (1 << 2)   /* Executable */
-#define SHF_TLS                     (1 << 10)  /* Section hold thread-local data.  */
-#define SHN_ABS                0xfff1          /* Associated symbol is absolute */
-#define SHN_COMMON     0xfff2          /* Associated symbol is common */
-#define R_X86_64_NONE          0       /* No reloc */
-#define R_X86_64_64            1       /* Direct 64 bit  */
-#define R_X86_64_PC32          2       /* PC relative 32 bit signed */
-#define R_X86_64_GOT32         3       /* 32 bit GOT entry */
-#define R_X86_64_PLT32         4       /* 32 bit PLT address */
-#define R_X86_64_COPY          5       /* Copy symbol at runtime */
-#define R_X86_64_GLOB_DAT      6       /* Create GOT entry */
-#define R_X86_64_JUMP_SLOT     7       /* Create PLT entry */
-#define R_X86_64_RELATIVE      8       /* Adjust by program base */
-#define R_X86_64_GOTPCREL      9       /* 32 bit signed PC relative
-                                          offset to GOT */
-#define R_X86_64_32            10      /* Direct 32 bit zero extended */
-#define R_X86_64_32S           11      /* Direct 32 bit sign extended */
-#define R_X86_64_16            12      /* Direct 16 bit zero extended */
-#define R_X86_64_PC16          13      /* 16 bit sign extended pc relative */
-#define R_X86_64_8             14      /* Direct 8 bit sign extended  */
-#define R_X86_64_PC8           15      /* 8 bit sign extended pc relative */
-#define R_X86_64_DTPMOD64      16      /* ID of module containing symbol */
-#define R_X86_64_DTPOFF64      17      /* Offset in module's TLS block */
-#define R_X86_64_TPOFF64       18      /* Offset in initial TLS block */
-#define R_X86_64_TLSGD         19      /* 32 bit signed PC relative offset
-                                          to two GOT entries for GD symbol */
-#define R_X86_64_TLSLD         20      /* 32 bit signed PC relative offset
-                                          to two GOT entries for LD symbol */
-#define R_X86_64_DTPOFF32      21      /* Offset in TLS block */
-#define R_X86_64_GOTTPOFF      22      /* 32 bit signed PC relative offset
-                                          to GOT entry for IE symbol */
-#define R_X86_64_TPOFF32       23      /* Offset in initial TLS block */
-#define R_X86_64_PC64          24      /* word64 S + A - P */
-#define R_X86_64_GOTOFF64      25      /* word64 S + A - GOT */
-#define R_X86_64_GOTPC32       26      /* word32 GOT + A - P */
-#define R_X86_64_GOT64         27      /* word64 G + A */
-#define R_X86_64_GOTPCREL64    28      /* word64 G + GOT - P + A */
-#define R_X86_64_GOTPC64       29      /* word64 GOT - P + A */
-#define R_X86_64_GOTPLT64      30      /* word64 G + A */
-#define R_X86_64_PLTOFF64      31      /* word64 L - GOT + A */
-#define R_X86_64_SIZE32                32      /* word32 Z + A */
-#define R_X86_64_SIZE64                33      /* word64 Z + A */
-#define R_X86_64_GOTPC32_TLSDESC 34    /* word32 */
-#define R_X86_64_TLSDESC_CALL  35      /* none */
-#define R_X86_64_TLSDESC       36      /* word64×2 */
-#define ET_REL         1               /* Relocatable file */
-#define EM_X86_64      62              /* AMD x86-64 architecture */
-#define STT_NOTYPE     0               /* Symbol type is unspecified */
-#define STT_OBJECT     1               /* Symbol is a data object */
-#define STT_FUNC       2               /* Symbol is a code object */
-#define STT_SECTION    3               /* Symbol associated with a section */
-#define STT_FILE       4               /* Symbol's name is file name */
-#define STT_COMMON     5               /* Symbol is a common data object */
-#define STT_TLS                6               /* Symbol is thread-local data object*/
-#define        STT_NUM         7               /* Number of defined types.  */
-
-/* Definitions in lieu of dwarf.h */
-#define    DW_TAG_compile_unit   0x11
-#define    DW_TAG_subprogram   0x2e
-#define    DW_AT_name   0x03
-#define    DW_AT_stmt_list   0x10
-#define    DW_AT_low_pc   0x11
-#define    DW_AT_high_pc   0x12
-#define    DW_AT_language  0x13
-#define    DW_AT_producer   0x25
-#define    DW_AT_frame_base   0x40
-#define    DW_FORM_addr   0x01
-#define    DW_FORM_data2   0x05
-#define    DW_FORM_data4   0x06
-#define    DW_FORM_string   0x08
-#define    DW_LNS_extended_op  0
-#define    DW_LNS_advance_pc   2
-#define    DW_LNS_advance_line   3
-#define    DW_LNS_set_file   4
-#define    DW_LNE_end_sequence   1
-#define    DW_LNE_set_address   2
-#define    DW_LNE_define_file   3
-#define    DW_LANG_Mips_Assembler  0x8001
-
-#define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
-
-typedef uint32_t Elf64_Word;
-typedef uint64_t Elf64_Xword;
-typedef uint64_t Elf64_Addr;
-typedef uint64_t Elf64_Off;
-typedef struct
-{
-  Elf64_Word   sh_name;        /* Section name (string tbl index) */
-  Elf64_Word   sh_type;        /* Section type */
-  Elf64_Xword  sh_flags;       /* Section flags */
-  Elf64_Addr   sh_addr;        /* Section virtual addr at execution */
-  Elf64_Off    sh_offset;      /* Section file offset */
-  Elf64_Xword  sh_size;        /* Section size in bytes */
-  Elf64_Word   sh_link;        /* Link to another section */
-  Elf64_Word   sh_info;        /* Additional section information */
-  Elf64_Xword  sh_addralign;   /* Section alignment */
-  Elf64_Xword  sh_entsize;     /* Entry size if section holds table */
-} Elf64_Shdr;
-
+#include "elf64.h"
+#include "dwarf.h"
+#include "outelf.h"
 
 #ifdef OF_ELF64
 
+#define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
 
 struct Reloc {
     struct Reloc *next;
@@ -199,22 +97,6 @@ static uint8_t elf_abiver = 0;      /* Current ABI version */
 
 extern struct ofmt of_elf64;
 
-#define SHN_UNDEF 0
-
-#define SYM_GLOBAL 0x10
-
-#define STV_DEFAULT 0
-#define STV_INTERNAL 1
-#define STV_HIDDEN 2
-#define STV_PROTECTED 3
-
-#define GLOBAL_TEMP_BASE 1048576     /* bigger than any reasonable sym id */
-
-#define SEG_ALIGN 16            /* alignment of sections in file */
-#define SEG_ALIGN_1 (SEG_ALIGN-1)
-
-#define TY_DEBUGSYMLIN 0x40     /* internal call to debug_out */
-
 static struct ELF_SECTDATA {
     void *data;
     int64_t len;
@@ -454,9 +336,9 @@ static int elf_make_section(char *name, int type, int flags, int align)
 static int32_t elf_section_names(char *name, int pass, int *bits)
 {
     char *p;
-    unsigned flags_and, flags_or;
-    uint64_t type, align;
-    int i;
+    uint32_t flags, flags_and, flags_or;
+    uint64_t align;
+    int type, i;
 
     /*
      * Default is 64 bits.
@@ -522,9 +404,9 @@ static int32_t elf_section_names(char *name, int pass, int *bits)
                   " declaration of section `%s'", q, name);
     }
 
-    if (!strcmp(name, ".comment") ||
-        !strcmp(name, ".shstrtab") ||
-        !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
+    if (!strcmp(name, ".shstrtab") ||
+        !strcmp(name, ".symtab") ||
+        !strcmp(name, ".strtab")) {
         error(ERR_NONFATAL, "attempt to redefine reserved section"
               "name `%s'", name);
         return NO_SEG;
@@ -534,31 +416,19 @@ static int32_t elf_section_names(char *name, int pass, int *bits)
         if (!strcmp(name, sects[i]->name))
             break;
     if (i == nsects) {
-        if (!strcmp(name, ".text"))
-            i = elf_make_section(name, SHT_PROGBITS,
-                                 SHF_ALLOC | SHF_EXECINSTR, 16);
-        else if (!strcmp(name, ".rodata"))
-            i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
-        else if (!strcmp(name, ".data"))
-            i = elf_make_section(name, SHT_PROGBITS,
-                                 SHF_ALLOC | SHF_WRITE, 4);
-        else if (!strcmp(name, ".bss"))
-            i = elf_make_section(name, SHT_NOBITS,
-                                 SHF_ALLOC | SHF_WRITE, 4);
-        else if (!strcmp(name, ".tdata"))
-            i = elf_make_section(name, SHT_PROGBITS,
-                                 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
-        else if (!strcmp(name, ".tbss"))
-            i = elf_make_section(name, SHT_NOBITS,
-                                 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
-        else
-            i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
-        if (type)
-            sects[i]->type = type;
-        if (align)
-            sects[i]->align = align;
-        sects[i]->flags &= ~flags_and;
-        sects[i]->flags |= flags_or;
+       const struct elf_known_section *ks = elf_known_sections;
+
+       while (ks->name) {
+           if (!strcmp(name, ks->name))
+               break;
+           ks++;
+       }
+
+       type = type ? type : ks->type;
+       align = align ? align : ks->align;
+       flags = (ks->flags & ~flags_and) | flags_or;
+
+       i = elf_make_section(name, type, flags, align);
     } else if (pass == 1) {
           if ((type && sects[i]->type != type)
              || (align && sects[i]->align != align)
@@ -1155,8 +1025,6 @@ static void elf_write(void)
     int align;
     int scount;
     char *p;
-    int commlen;
-    char comment[64];
     int i;
 
     struct SAA *symtab;
@@ -1164,18 +1032,16 @@ static void elf_write(void)
 
     /*
      * Work out how many sections we will have. We have SHN_UNDEF,
-     * then the flexible user sections, then the four fixed
-     * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
-     * then optionally relocation sections for the user sections.
+     * then the flexible user sections, then the fixed sections
+     * `.shstrtab', `.symtab' and `.strtab', then optionally
+     * relocation sections for the user sections.
      */
+    nsections = 4;
     if (of_elf64.current_dfmt == &df_stabs)
-        nsections = 8;
+        nsections += 3;
     else if (of_elf64.current_dfmt == &df_dwarf)
-        nsections = 15;
-    else
-        nsections = 5;          /* SHN_UNDEF and the fixed ones */
+        nsections += 10;
 
-    add_sectname("", ".comment");
     add_sectname("", ".shstrtab");
     add_sectname("", ".symtab");
     add_sectname("", ".strtab");
@@ -1215,12 +1081,6 @@ static void elf_write(void)
     }
 
     /*
-     * Do the comment.
-     */
-    *comment = '\0';
-    commlen = 2 + snprintf(comment+1, sizeof comment-1, "%s", nasm_comment);
-
-    /*
      * Output the ELF header.
      */
     fwrite("\177ELF\2\1\1", 7, 1, elffp);
@@ -1272,9 +1132,6 @@ static void elf_write(void)
         p += strlen(p) + 1;
         scount++;               /* ditto */
     }
-    elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0);  /* .comment */
-    scount++;                   /* ditto */
-    p += strlen(p) + 1;
     elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0);     /* .shstrtab */
     scount++;                   /* ditto */
     p += strlen(p) + 1;
@@ -1623,7 +1480,7 @@ static int elf_set_info(enum geninfo type, char **val)
     return 0;
 }
 static struct dfmt df_dwarf = {
-    "ELF64 (X86_64) dwarf debug format for Linux",
+    "ELF64 (x86-64) dwarf debug format for Linux/Unix",
     "dwarf",
     debug64_init,
     dwarf64_linenum,
@@ -1634,7 +1491,7 @@ static struct dfmt df_dwarf = {
     dwarf64_cleanup
 };
 static struct dfmt df_stabs = {
-    "ELF64 (X86_64) stabs debug format for Linux",
+    "ELF64 (x86-64) stabs debug format for Linux/Unix",
     "stabs",
     debug64_init,
     stabs64_linenum,