Skip to content

Conversation

@AsterNighT
Copy link

@AsterNighT AsterNighT commented Oct 29, 2025

Summary

Sort the sections in a segment when loading to support go ELF files.

Background

Go ELF files have a section named .note.go.buildid, it starts before .text (at offset f9c, while .text start at 1000), but its index in section table (which is 19) is after index of .text (which is 1). The .note.go.buildid section should be placed before .text but since the index is reversed, ELFIO try to place .text first, which cause an error in write_segment_data.

For now, simply loading and saving a go ELF without modifying it yields an error while saving.

What does this PR do

Sort the sections in each segment according to their offsets.

Test passed 73/73

@serge1
Copy link
Owner

serge1 commented Nov 6, 2025

Hi,
This is an interesting observation. Generally, I am not objecting it. But, would it be possible to provide a simple Go ELF file that demonstrates the problem?
Appreciate your answer and Best Regards

@AsterNighT
Copy link
Author

Sorry for the late reply. Here is the go binary. Not exactly the same one causing me trouble, but the section is the same.
go-binary.zip

The source code is

package main

import "fmt"

func main() {
        fmt.Println("Hello, World!")
}

go version go1.18.1 linux/amd64

readelf -S gives

There are 23 section headers, starting at offset 0x1c8:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         0000000000401000  00001000
       000000000007d3a7  0000000000000000  AX       0     0     32
  [ 2] .rodata           PROGBITS         000000000047f000  0007f000
       000000000003522c  0000000000000000   A       0     0     32
  [ 3] .shstrtab         STRTAB           0000000000000000  000b4240
       000000000000017a  0000000000000000           0     0     1
  [ 4] .typelink         PROGBITS         00000000004b43c0  000b43c0
       00000000000004c0  0000000000000000   A       0     0     32
  [ 5] .itablink         PROGBITS         00000000004b4880  000b4880
       0000000000000058  0000000000000000   A       0     0     32
  [ 6] .gosymtab         PROGBITS         00000000004b48d8  000b48d8
       0000000000000000  0000000000000000   A       0     0     1
  [ 7] .gopclntab        PROGBITS         00000000004b48e0  000b48e0
       0000000000053bb0  0000000000000000   A       0     0     32
  [ 8] .go.buildinfo     PROGBITS         0000000000509000  00109000
       0000000000000110  0000000000000000  WA       0     0     16
  [ 9] .noptrdata        PROGBITS         0000000000509120  00109120
       00000000000105e0  0000000000000000  WA       0     0     32
  [10] .data             PROGBITS         0000000000519700  00119700
       0000000000007810  0000000000000000  WA       0     0     32
  [11] .bss              NOBITS           0000000000520f20  00120f20
       000000000002ef60  0000000000000000  WA       0     0     32
  [12] .noptrbss         NOBITS           000000000054fe80  0014fe80
       00000000000051a0  0000000000000000  WA       0     0     32
  [13] .zdebug_abbrev    PROGBITS         0000000000556000  00121000
       0000000000000127  0000000000000000           0     0     1
  [14] .zdebug_line      PROGBITS         0000000000556127  00121127
       000000000001b25a  0000000000000000           0     0     1
  [15] .zdebug_frame     PROGBITS         0000000000571381  0013c381
       00000000000054dc  0000000000000000           0     0     1
  [16] .debug_gdb_s[...] PROGBITS         000000000057685d  0014185d
       000000000000002d  0000000000000000           0     0     1
  [17] .zdebug_info      PROGBITS         000000000057688a  0014188a
       00000000000335e0  0000000000000000           0     0     1
  [18] .zdebug_loc       PROGBITS         00000000005a9e6a  00174e6a
       000000000001a26e  0000000000000000           0     0     1
  [19] .zdebug_ranges    PROGBITS         00000000005c40d8  0018f0d8
       0000000000008988  0000000000000000           0     0     1
  [20] .note.go.buildid  NOTE             0000000000400f9c  00000f9c
       0000000000000064  0000000000000000   A       0     0     4
  [21] .symtab           SYMTAB           0000000000000000  00197a60
       000000000000c0f0  0000000000000018          22    85     8
  [22] .strtab           STRTAB           0000000000000000  001a3b50
       000000000000ac8e  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  D (mbind), l (large), p (processor specific)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants