在可执行文件中添加段(Section)

我们以可执行src文件为例
删除所有的符号和重定位信息:

 strip -s src

查看段(Section)头部信息:

root@000d3fada0b3:~/go/src# readelf -S src
There are 13 section headers, starting at offset 0x127768:

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
       0000000000081258  0000000000000000  AX       0     0     16
  [ 2] .rodata           PROGBITS         0000000000483000  00083000
       0000000000041a5b  0000000000000000   A       0     0     32
  [ 3] .typelink         PROGBITS         00000000004c4b80  000c4b80
       0000000000000b48  0000000000000000   A       0     0     32
  [ 4] .itablink         PROGBITS         00000000004c56c8  000c56c8
       0000000000000040  0000000000000000   A       0     0     8
  [ 5] .gosymtab         PROGBITS         00000000004c5708  000c5708
       0000000000000000  0000000000000000   A       0     0     1
  [ 6] .gopclntab        PROGBITS         00000000004c5720  000c5720
       000000000004e0e7  0000000000000000   A       0     0     32
  [ 7] .noptrdata        PROGBITS         0000000000514000  00114000
       000000000000cbdc  0000000000000000  WA       0     0     32
  [ 8] .data             PROGBITS         0000000000520be0  00120be0
       0000000000006b10  0000000000000000  WA       0     0     32
  [ 9] .bss              NOBITS           0000000000527700  001276f0
       000000000001c688  0000000000000000  WA       0     0     32
  [10] .noptrbss         NOBITS           0000000000543da0  001276f0
       0000000000002698  0000000000000000  WA       0     0     32
  [11] .note.go.buildid  NOTE             0000000000400f9c  00000f9c
       0000000000000064  0000000000000000   A       0     0     4
  [12] .shstrtab         STRTAB           0000000000000000  001276f0
       0000000000000073  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),
  l (large), p (processor specific)

添加一个新的段(Section),并写入信息。之后生成一个新的可执行文件src2

root@000d3fada0b3:~/go/src# objcopy --add-section .xxx=o.md --set-section-flags .xxx=noload,readonly src src2

查看src2的段(Section)头部的信息:

root@000d3fada0b3:~/go/src# readelf -S src2
There are 14 section headers, starting at offset 0x128900:

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
       0000000000081258  0000000000000000  AX       0     0     16
  [ 2] .rodata           PROGBITS         0000000000483000  00083000
       0000000000041a5b  0000000000000000   A       0     0     32
  [ 3] .typelink         PROGBITS         00000000004c4b80  000c4b80
       0000000000000b48  0000000000000000   A       0     0     32
  [ 4] .itablink         PROGBITS         00000000004c56c8  000c56c8
       0000000000000040  0000000000000000   A       0     0     8
  [ 5] .gosymtab         PROGBITS         00000000004c5708  000c5708
       0000000000000000  0000000000000000   A       0     0     1
  [ 6] .gopclntab        PROGBITS         00000000004c5720  000c5720
       000000000004e0e7  0000000000000000   A       0     0     32
  [ 7] .noptrdata        PROGBITS         0000000000514000  00114000
       000000000000cbdc  0000000000000000  WA       0     0     32
  [ 8] .data             PROGBITS         0000000000520be0  00120be0
       0000000000006b10  0000000000000000  WA       0     0     32
  [ 9] .bss              NOBITS           0000000000527700  001276f0
       000000000001c688  0000000000000000  WA       0     0     32
  [10] .noptrbss         NOBITS           0000000000543da0  001276f0
       0000000000002698  0000000000000000  WA       0     0     32
  [11] .note.go.buildid  NOTE             0000000000400f9c  00000f9c
       0000000000000064  0000000000000000   A       0     0     4
  [12] .xxx              PROGBITS         0000000000000000  001276f0
       0000000000001191  0000000000000000           0     0     1
  [13] .shstrtab         STRTAB           0000000000000000  00128881
       0000000000000078  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),
  l (large), p (processor specific)

src段(Section)信息相对比,发现多了一个.xxx的段。

其他命令

更新段(Section)信息:

objcopy --update-section .abc=data2 dst

重命名:

objcopy --rename-section .abc=.demo dst

删除段信息:

objcopy --remove-section .demo dst

你可能感兴趣的:(在可执行文件中添加段(Section))