Skip to content

Commit 5bb73e6

Browse files
mengzhuogopherbot
authored andcommitted
debug/elf: add riscv attributes definitions
This CL adds `riscv.attributes` related ELF section header type and program header type according to [RISC-V ELF Specification](https://github.com/riscv-non-isa/riscv-elf-psabi-doc/releases/download/v1.0/riscv-abi.pdf) Also an riscv64/linux testcase binary built from: ``` gcc -march=rv64g -no-pie -o gcc-riscv64-linux-exec hello.c strip gcc-riscv64-linux-exec ``` Fixes #72843 Change-Id: I7710a0516f69141c0efaba71dd997f05b4c88421 Reviewed-on: https://go-review.googlesource.com/c/go/+/657515 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 853b514 commit 5bb73e6

File tree

5 files changed

+89
-30
lines changed

5 files changed

+89
-30
lines changed

api/next/72843.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pkg debug/elf, const PT_RISCV_ATTRIBUTES = 1879048195 #72843
2+
pkg debug/elf, const PT_RISCV_ATTRIBUTES ProgType #72843
3+
pkg debug/elf, const SHT_RISCV_ATTRIBUTES = 1879048195 #72843
4+
pkg debug/elf, const SHT_RISCV_ATTRIBUTES SectionType #72843
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The [debug/elf] package adds two new constants:
2+
- [PT_RISCV_ATTRIBUTES]
3+
- [SHT_RISCV_ATTRIBUTES]
4+
for RISC-V ELF parsing.

src/debug/elf/elf.go

+35-30
Original file line numberDiff line numberDiff line change
@@ -620,36 +620,37 @@ func (i SectionIndex) GoString() string { return stringName(uint32(i), shnString
620620
type SectionType uint32
621621

622622
const (
623-
SHT_NULL SectionType = 0 /* inactive */
624-
SHT_PROGBITS SectionType = 1 /* program defined information */
625-
SHT_SYMTAB SectionType = 2 /* symbol table section */
626-
SHT_STRTAB SectionType = 3 /* string table section */
627-
SHT_RELA SectionType = 4 /* relocation section with addends */
628-
SHT_HASH SectionType = 5 /* symbol hash table section */
629-
SHT_DYNAMIC SectionType = 6 /* dynamic section */
630-
SHT_NOTE SectionType = 7 /* note section */
631-
SHT_NOBITS SectionType = 8 /* no space section */
632-
SHT_REL SectionType = 9 /* relocation section - no addends */
633-
SHT_SHLIB SectionType = 10 /* reserved - purpose unknown */
634-
SHT_DYNSYM SectionType = 11 /* dynamic symbol table section */
635-
SHT_INIT_ARRAY SectionType = 14 /* Initialization function pointers. */
636-
SHT_FINI_ARRAY SectionType = 15 /* Termination function pointers. */
637-
SHT_PREINIT_ARRAY SectionType = 16 /* Pre-initialization function ptrs. */
638-
SHT_GROUP SectionType = 17 /* Section group. */
639-
SHT_SYMTAB_SHNDX SectionType = 18 /* Section indexes (see SHN_XINDEX). */
640-
SHT_LOOS SectionType = 0x60000000 /* First of OS specific semantics */
641-
SHT_GNU_ATTRIBUTES SectionType = 0x6ffffff5 /* GNU object attributes */
642-
SHT_GNU_HASH SectionType = 0x6ffffff6 /* GNU hash table */
643-
SHT_GNU_LIBLIST SectionType = 0x6ffffff7 /* GNU prelink library list */
644-
SHT_GNU_VERDEF SectionType = 0x6ffffffd /* GNU version definition section */
645-
SHT_GNU_VERNEED SectionType = 0x6ffffffe /* GNU version needs section */
646-
SHT_GNU_VERSYM SectionType = 0x6fffffff /* GNU version symbol table */
647-
SHT_HIOS SectionType = 0x6fffffff /* Last of OS specific semantics */
648-
SHT_LOPROC SectionType = 0x70000000 /* reserved range for processor */
649-
SHT_MIPS_ABIFLAGS SectionType = 0x7000002a /* .MIPS.abiflags */
650-
SHT_HIPROC SectionType = 0x7fffffff /* specific section header types */
651-
SHT_LOUSER SectionType = 0x80000000 /* reserved range for application */
652-
SHT_HIUSER SectionType = 0xffffffff /* specific indexes */
623+
SHT_NULL SectionType = 0 /* inactive */
624+
SHT_PROGBITS SectionType = 1 /* program defined information */
625+
SHT_SYMTAB SectionType = 2 /* symbol table section */
626+
SHT_STRTAB SectionType = 3 /* string table section */
627+
SHT_RELA SectionType = 4 /* relocation section with addends */
628+
SHT_HASH SectionType = 5 /* symbol hash table section */
629+
SHT_DYNAMIC SectionType = 6 /* dynamic section */
630+
SHT_NOTE SectionType = 7 /* note section */
631+
SHT_NOBITS SectionType = 8 /* no space section */
632+
SHT_REL SectionType = 9 /* relocation section - no addends */
633+
SHT_SHLIB SectionType = 10 /* reserved - purpose unknown */
634+
SHT_DYNSYM SectionType = 11 /* dynamic symbol table section */
635+
SHT_INIT_ARRAY SectionType = 14 /* Initialization function pointers. */
636+
SHT_FINI_ARRAY SectionType = 15 /* Termination function pointers. */
637+
SHT_PREINIT_ARRAY SectionType = 16 /* Pre-initialization function ptrs. */
638+
SHT_GROUP SectionType = 17 /* Section group. */
639+
SHT_SYMTAB_SHNDX SectionType = 18 /* Section indexes (see SHN_XINDEX). */
640+
SHT_LOOS SectionType = 0x60000000 /* First of OS specific semantics */
641+
SHT_GNU_ATTRIBUTES SectionType = 0x6ffffff5 /* GNU object attributes */
642+
SHT_GNU_HASH SectionType = 0x6ffffff6 /* GNU hash table */
643+
SHT_GNU_LIBLIST SectionType = 0x6ffffff7 /* GNU prelink library list */
644+
SHT_GNU_VERDEF SectionType = 0x6ffffffd /* GNU version definition section */
645+
SHT_GNU_VERNEED SectionType = 0x6ffffffe /* GNU version needs section */
646+
SHT_GNU_VERSYM SectionType = 0x6fffffff /* GNU version symbol table */
647+
SHT_HIOS SectionType = 0x6fffffff /* Last of OS specific semantics */
648+
SHT_LOPROC SectionType = 0x70000000 /* reserved range for processor */
649+
SHT_RISCV_ATTRIBUTES SectionType = 0x70000003 /* RISCV object attributes */
650+
SHT_MIPS_ABIFLAGS SectionType = 0x7000002a /* .MIPS.abiflags */
651+
SHT_HIPROC SectionType = 0x7fffffff /* specific section header types */
652+
SHT_LOUSER SectionType = 0x80000000 /* reserved range for application */
653+
SHT_HIUSER SectionType = 0xffffffff /* specific indexes */
653654
)
654655

655656
var shtStrings = []intName{
@@ -678,6 +679,8 @@ var shtStrings = []intName{
678679
{0x6ffffffe, "SHT_GNU_VERNEED"},
679680
{0x6fffffff, "SHT_GNU_VERSYM"},
680681
{0x70000000, "SHT_LOPROC"},
682+
// We don't list the processor-dependent SectionType,
683+
// as the values overlap.
681684
{0x7000002a, "SHT_MIPS_ABIFLAGS"},
682685
{0x7fffffff, "SHT_HIPROC"},
683686
{0x80000000, "SHT_LOUSER"},
@@ -794,6 +797,8 @@ const (
794797
PT_MIPS_OPTIONS ProgType = 0x70000002 /* Options */
795798
PT_MIPS_ABIFLAGS ProgType = 0x70000003 /* ABI flags */
796799

800+
PT_RISCV_ATTRIBUTES ProgType = 0x70000003 /* RISC-V ELF attribute section. */
801+
797802
PT_S390_PGSTE ProgType = 0x70000000 /* 4k page table size */
798803

799804
PT_HIPROC ProgType = 0x7fffffff /* Last processor-specific type. */

src/debug/elf/file_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,52 @@ var fileTests = []fileTest{
447447
{"puts", 16, 0, false, 0, 0, 0, 0, "", ""},
448448
},
449449
},
450+
{
451+
"testdata/gcc-riscv64-linux-exec",
452+
FileHeader{ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ELFOSABI_NONE, 0, binary.LittleEndian, ET_EXEC, EM_RISCV, 0x10460},
453+
[]SectionHeader{
454+
{"", SHT_NULL, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
455+
{".interp", SHT_PROGBITS, SHF_ALLOC, 0x10270, 0x270, 0x21, 0x0, 0x0, 0x1, 0x0, 0x21},
456+
{".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, 0x10294, 0x294, 0x24, 0x0, 0x0, 0x4, 0x0, 0x24},
457+
{".note.ABI-tag", SHT_NOTE, SHF_ALLOC, 0x102b8, 0x2b8, 0x20, 0x0, 0x0, 0x4, 0x0, 0x20},
458+
{".gnu.hash", SHT_GNU_HASH, SHF_ALLOC, 0x102d8, 0x2d8, 0x30, 0x5, 0x0, 0x8, 0x0, 0x30},
459+
{".dynsym", SHT_DYNSYM, SHF_ALLOC, 0x10308, 0x308, 0x60, 0x6, 0x1, 0x8, 0x18, 0x60},
460+
{".dynstr", SHT_STRTAB, SHF_ALLOC, 0x10368, 0x368, 0x4a, 0x0, 0x0, 0x1, 0x0, 0x4a},
461+
{".gnu.version", SHT_GNU_VERSYM, SHF_ALLOC, 0x103b2, 0x3b2, 0x8, 0x5, 0x0, 0x2, 0x2, 0x8},
462+
{".gnu.version_r", SHT_GNU_VERNEED, SHF_ALLOC, 0x103c0, 0x3c0, 0x30, 0x6, 0x1, 0x8, 0x0, 0x30},
463+
{".rela.plt", SHT_RELA, SHF_ALLOC + SHF_INFO_LINK, 0x103f0, 0x3f0, 0x30, 0x5, 0x14, 0x8, 0x18, 0x30},
464+
{".plt", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR, 0x10420, 0x420, 0x40, 0x0, 0x0, 0x10, 0x10, 0x40},
465+
{".text", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR, 0x10460, 0x460, 0xd8, 0x0, 0x0, 0x4, 0x0, 0xd8},
466+
{".rodata", SHT_PROGBITS, SHF_ALLOC, 0x10538, 0x538, 0x15, 0x0, 0x0, 0x8, 0x0, 0x15},
467+
{".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, 0x10550, 0x550, 0x24, 0x0, 0x0, 0x4, 0x0, 0x24},
468+
{".eh_frame", SHT_PROGBITS, SHF_ALLOC, 0x10578, 0x578, 0x6c, 0x0, 0x0, 0x8, 0x0, 0x6c},
469+
{".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE + SHF_ALLOC, 0x11e00, 0xe00, 0x8, 0x0, 0x0, 0x1, 0x8, 0x8},
470+
{".init_array", SHT_INIT_ARRAY, SHF_WRITE + SHF_ALLOC, 0x11e08, 0xe08, 0x8, 0x0, 0x0, 0x8, 0x8, 0x8},
471+
{".fini_array", SHT_FINI_ARRAY, SHF_WRITE + SHF_ALLOC, 0x11e10, 0xe10, 0x8, 0x0, 0x0, 0x8, 0x8, 0x8},
472+
{".dynamic", SHT_DYNAMIC, SHF_WRITE + SHF_ALLOC, 0x11e18, 0xe18, 0x1d0, 0x6, 0x0, 0x8, 0x10, 0x1d0},
473+
{".got", SHT_PROGBITS, SHF_WRITE + SHF_ALLOC, 0x11fe8, 0xfe8, 0x8, 0x0, 0x0, 0x8, 0x8, 0x8},
474+
{".got.plt", SHT_PROGBITS, SHF_WRITE + SHF_ALLOC, 0x11ff0, 0xff0, 0x20, 0x0, 0x0, 0x8, 0x8, 0x20},
475+
{".sdata", SHT_PROGBITS, SHF_WRITE + SHF_ALLOC, 0x12010, 0x1010, 0x8, 0x0, 0x0, 0x8, 0x0, 0x8},
476+
{".bss", SHT_NOBITS, SHF_WRITE + SHF_ALLOC, 0x12018, 0x1018, 0x8, 0x0, 0x0, 0x1, 0x0, 0x8},
477+
{".comment", SHT_PROGBITS, SHF_MERGE + SHF_STRINGS, 0x0, 0x1018, 0x26, 0x0, 0x0, 0x1, 0x1, 0x26},
478+
{".riscv.attributes", SHT_RISCV_ATTRIBUTES, 0x0, 0x0, 0x103e, 0x66, 0x0, 0x0, 0x1, 0x0, 0x66},
479+
{".shstrtab", SHT_STRTAB, 0x0, 0x0, 0x10a4, 0xff, 0x0, 0x0, 0x1, 0x0, 0xff},
480+
},
481+
[]ProgHeader{
482+
{PT_PHDR, PF_R, 0x40, 0x10040, 0x10040, 0x230, 0x230, 0x8},
483+
{PT_INTERP, PF_R, 0x270, 0x10270, 0x10270, 0x21, 0x21, 0x1},
484+
{PT_RISCV_ATTRIBUTES, PF_R, 0x103e, 0x0, 0x0, 0x66, 0x0, 0x1},
485+
{PT_LOAD, PF_X + PF_R, 0x0, 0x10000, 0x10000, 0x5e4, 0x5e4, 0x1000},
486+
{PT_LOAD, PF_W + PF_R, 0xe00, 0x11e00, 0x11e00, 0x218, 0x220, 0x1000},
487+
{PT_DYNAMIC, PF_W + PF_R, 0xe18, 0x11e18, 0x11e18, 0x1d0, 0x1d0, 0x8},
488+
{PT_NOTE, PF_R, 0x294, 0x10294, 0x10294, 0x44, 0x44, 0x4},
489+
{PT_GNU_EH_FRAME, PF_R, 0x550, 0x10550, 0x10550, 0x24, 0x24, 0x4},
490+
{PT_GNU_STACK, PF_W + PF_R, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10},
491+
{PT_GNU_RELRO, PF_R, 0xe00, 0x11e00, 0x11e00, 0x200, 0x200, 0x1},
492+
},
493+
[]string{"libc.so.6"},
494+
nil,
495+
},
450496
}
451497

452498
func TestOpen(t *testing.T) {
6.04 KB
Binary file not shown.

0 commit comments

Comments
 (0)