| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package elf implements access to ELF object files. |
| 6 |
package elf |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"compress/zlib" |
| 11 |
"debug/dwarf" |
| 12 |
"encoding/binary" |
| 13 |
"errors" |
| 14 |
"fmt" |
| 15 |
"io" |
| 16 |
"os" |
| 17 |
"strings" |
| 18 |
) |
| 19 |
|
| 20 |
// seekStart, seekCurrent, seekEnd are copies of |
| 21 |
// io.SeekStart, io.SeekCurrent, and io.SeekEnd. |
| 22 |
// We can't use the ones from package io because |
| 23 |
// we want this code to build with Go 1.4 during |
| 24 |
// cmd/dist bootstrap. |
| 25 |
const ( |
| 26 |
seekStart int = 0 |
| 27 |
seekCurrent int = 1 |
| 28 |
seekEnd int = 2 |
| 29 |
) |
| 30 |
|
| 31 |
// TODO: error reporting detail |
| 32 |
|
| 33 |
/* |
| 34 |
* Internal ELF representation |
| 35 |
*/ |
| 36 |
|
| 37 |
// A FileHeader represents an ELF file header. |
| 38 |
type FileHeader struct { |
| 39 |
Class Class |
| 40 |
Data Data |
| 41 |
Version Version |
| 42 |
OSABI OSABI |
| 43 |
ABIVersion uint8 |
| 44 |
ByteOrder binary.ByteOrder |
| 45 |
Type Type |
| 46 |
Machine Machine |
| 47 |
Entry uint64 |
| 48 |
} |
| 49 |
|
| 50 |
// A File represents an open ELF file. |
| 51 |
type File struct { |
| 52 |
FileHeader |
| 53 |
Sections []*Section |
| 54 |
Progs []*Prog |
| 55 |
closer io.Closer |
| 56 |
gnuNeed []verneed |
| 57 |
gnuVersym []byte |
| 58 |
} |
| 59 |
|
| 60 |
// A SectionHeader represents a single ELF section header. |
| 61 |
type SectionHeader struct { |
| 62 |
Name string |
| 63 |
Type SectionType |
| 64 |
Flags SectionFlag |
| 65 |
Addr uint64 |
| 66 |
Offset uint64 |
| 67 |
Size uint64 |
| 68 |
Link uint32 |
| 69 |
Info uint32 |
| 70 |
Addralign uint64 |
| 71 |
Entsize uint64 |
| 72 |
|
| 73 |
// FileSize is the size of this section in the file in bytes. |
| 74 |
// If a section is compressed, FileSize is the size of the |
| 75 |
// compressed data, while Size (above) is the size of the |
| 76 |
// uncompressed data. |
| 77 |
FileSize uint64 |
| 78 |
} |
| 79 |
|
| 80 |
// A Section represents a single section in an ELF file. |
| 81 |
type Section struct { |
| 82 |
SectionHeader |
| 83 |
|
| 84 |
// Embed ReaderAt for ReadAt method. |
| 85 |
// Do not embed SectionReader directly |
| 86 |
// to avoid having Read and Seek. |
| 87 |
// If a client wants Read and Seek it must use |
| 88 |
// Open() to avoid fighting over the seek offset |
| 89 |
// with other clients. |
| 90 |
// |
| 91 |
// ReaderAt may be nil if the section is not easily available |
| 92 |
// in a random-access form. For example, a compressed section |
| 93 |
// may have a nil ReaderAt. |
| 94 |
io.ReaderAt |
| 95 |
sr *io.SectionReader |
| 96 |
|
| 97 |
compressionType CompressionType |
| 98 |
compressionOffset int64 |
| 99 |
} |
| 100 |
|
| 101 |
// Data reads and returns the contents of the ELF section. |
| 102 |
// Even if the section is stored compressed in the ELF file, |
| 103 |
// Data returns uncompressed data. |
| 104 |
func (s *Section) Data() ([]byte, error) { |
| 105 |
dat := make([]byte, s.Size) |
| 106 |
n, err := io.ReadFull(s.Open(), dat) |
| 107 |
return dat[0:n], err |
| 108 |
} |
| 109 |
|
| 110 |
// stringTable reads and returns the string table given by the |
| 111 |
// specified link value. |
| 112 |
func (f *File) stringTable(link uint32) ([]byte, error) { |
| 113 |
if link <= 0 || link >= uint32(len(f.Sections)) { |
| 114 |
return nil, errors.New("section has invalid string table link") |
| 115 |
} |
| 116 |
return f.Sections[link].Data() |
| 117 |
} |
| 118 |
|
| 119 |
// Open returns a new ReadSeeker reading the ELF section. |
| 120 |
// Even if the section is stored compressed in the ELF file, |
| 121 |
// the ReadSeeker reads uncompressed data. |
| 122 |
func (s *Section) Open() io.ReadSeeker { |
| 123 |
if s.Type == SHT_NOBITS { |
| 124 |
return io.NewSectionReader(&zeroReader{}, 0, int64(s.Size)) |
| 125 |
} |
| 126 |
if s.Flags&SHF_COMPRESSED == 0 { |
| 127 |
return io.NewSectionReader(s.sr, 0, 1<<63-1) |
| 128 |
} |
| 129 |
if s.compressionType == COMPRESS_ZLIB { |
| 130 |
return &readSeekerFromReader{ |
| 131 |
reset: func() (io.Reader, error) { |
| 132 |
fr := io.NewSectionReader(s.sr, s.compressionOffset, int64(s.FileSize)-s.compressionOffset) |
| 133 |
return zlib.NewReader(fr) |
| 134 |
}, |
| 135 |
size: int64(s.Size), |
| 136 |
} |
| 137 |
} |
| 138 |
err := &FormatError{int64(s.Offset), "unknown compression type", s.compressionType} |
| 139 |
return errorReader{err} |
| 140 |
} |
| 141 |
|
| 142 |
// A ProgHeader represents a single ELF program header. |
| 143 |
type ProgHeader struct { |
| 144 |
Type ProgType |
| 145 |
Flags ProgFlag |
| 146 |
Off uint64 |
| 147 |
Vaddr uint64 |
| 148 |
Paddr uint64 |
| 149 |
Filesz uint64 |
| 150 |
Memsz uint64 |
| 151 |
Align uint64 |
| 152 |
} |
| 153 |
|
| 154 |
// A Prog represents a single ELF program header in an ELF binary. |
| 155 |
type Prog struct { |
| 156 |
ProgHeader |
| 157 |
|
| 158 |
// Embed ReaderAt for ReadAt method. |
| 159 |
// Do not embed SectionReader directly |
| 160 |
// to avoid having Read and Seek. |
| 161 |
// If a client wants Read and Seek it must use |
| 162 |
// Open() to avoid fighting over the seek offset |
| 163 |
// with other clients. |
| 164 |
io.ReaderAt |
| 165 |
sr *io.SectionReader |
| 166 |
} |
| 167 |
|
| 168 |
// Open returns a new ReadSeeker reading the ELF program body. |
| 169 |
func (p *Prog) Open() io.ReadSeeker { return io.NewSectionReader(p.sr, 0, 1<<63-1) } |
| 170 |
|
| 171 |
// A Symbol represents an entry in an ELF symbol table section. |
| 172 |
type Symbol struct { |
| 173 |
Name string |
| 174 |
Info, Other byte |
| 175 |
Section SectionIndex |
| 176 |
Value, Size uint64 |
| 177 |
|
| 178 |
// Version and Library are present only for the dynamic symbol |
| 179 |
// table. |
| 180 |
Version string |
| 181 |
Library string |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* ELF reader |
| 186 |
*/ |
| 187 |
|
| 188 |
type FormatError struct { |
| 189 |
off int64 |
| 190 |
msg string |
| 191 |
val any |
| 192 |
} |
| 193 |
|
| 194 |
func (e *FormatError) Error() string { |
| 195 |
msg := e.msg |
| 196 |
if e.val != nil { |
| 197 |
msg += fmt.Sprintf(" '%v' ", e.val) |
| 198 |
} |
| 199 |
msg += fmt.Sprintf("in record at byte %#x", e.off) |
| 200 |
return msg |
| 201 |
} |
| 202 |
|
| 203 |
// Open opens the named file using os.Open and prepares it for use as an ELF binary. |
| 204 |
func Open(name string) (*File, error) { |
| 205 |
f, err := os.Open(name) |
| 206 |
if err != nil { |
| 207 |
return nil, err |
| 208 |
} |
| 209 |
ff, err := NewFile(f) |
| 210 |
if err != nil { |
| 211 |
f.Close() |
| 212 |
return nil, err |
| 213 |
} |
| 214 |
ff.closer = f |
| 215 |
return ff, nil |
| 216 |
} |
| 217 |
|
| 218 |
// Close closes the File. |
| 219 |
// If the File was created using NewFile directly instead of Open, |
| 220 |
// Close has no effect. |
| 221 |
func (f *File) Close() error { |
| 222 |
var err error |
| 223 |
if f.closer != nil { |
| 224 |
err = f.closer.Close() |
| 225 |
f.closer = nil |
| 226 |
} |
| 227 |
return err |
| 228 |
} |
| 229 |
|
| 230 |
// SectionByType returns the first section in f with the |
| 231 |
// given type, or nil if there is no such section. |
| 232 |
func (f *File) SectionByType(typ SectionType) *Section { |
| 233 |
for _, s := range f.Sections { |
| 234 |
if s.Type == typ { |
| 235 |
return s |
| 236 |
} |
| 237 |
} |
| 238 |
return nil |
| 239 |
} |
| 240 |
|
| 241 |
// NewFile creates a new File for accessing an ELF binary in an underlying reader. |
| 242 |
// The ELF binary is expected to start at position 0 in the ReaderAt. |
| 243 |
func NewFile(r io.ReaderAt) (*File, error) { |
| 244 |
sr := io.NewSectionReader(r, 0, 1<<63-1) |
| 245 |
// Read and decode ELF identifier |
| 246 |
var ident [16]uint8 |
| 247 |
if _, err := r.ReadAt(ident[0:], 0); err != nil { |
| 248 |
return nil, err |
| 249 |
} |
| 250 |
if ident[0] != '\x7f' || ident[1] != 'E' || ident[2] != 'L' || ident[3] != 'F' { |
| 251 |
return nil, &FormatError{0, "bad magic number", ident[0:4]} |
| 252 |
} |
| 253 |
|
| 254 |
f := new(File) |
| 255 |
f.Class = Class(ident[EI_CLASS]) |
| 256 |
switch f.Class { |
| 257 |
case ELFCLASS32: |
| 258 |
case ELFCLASS64: |
| 259 |
// ok |
| 260 |
default: |
| 261 |
return nil, &FormatError{0, "unknown ELF class", f.Class} |
| 262 |
} |
| 263 |
|
| 264 |
f.Data = Data(ident[EI_DATA]) |
| 265 |
switch f.Data { |
| 266 |
case ELFDATA2LSB: |
| 267 |
f.ByteOrder = binary.LittleEndian |
| 268 |
case ELFDATA2MSB: |
| 269 |
f.ByteOrder = binary.BigEndian |
| 270 |
default: |
| 271 |
return nil, &FormatError{0, "unknown ELF data encoding", f.Data} |
| 272 |
} |
| 273 |
|
| 274 |
f.Version = Version(ident[EI_VERSION]) |
| 275 |
if f.Version != EV_CURRENT { |
| 276 |
return nil, &FormatError{0, "unknown ELF version", f.Version} |
| 277 |
} |
| 278 |
|
| 279 |
f.OSABI = OSABI(ident[EI_OSABI]) |
| 280 |
f.ABIVersion = ident[EI_ABIVERSION] |
| 281 |
|
| 282 |
// Read ELF file header |
| 283 |
var phoff int64 |
| 284 |
var phentsize, phnum int |
| 285 |
var shoff int64 |
| 286 |
var shentsize, shnum, shstrndx int |
| 287 |
switch f.Class { |
| 288 |
case ELFCLASS32: |
| 289 |
hdr := new(Header32) |
| 290 |
sr.Seek(0, seekStart) |
| 291 |
if err := binary.Read(sr, f.ByteOrder, hdr); err != nil { |
| 292 |
return nil, err |
| 293 |
} |
| 294 |
f.Type = Type(hdr.Type) |
| 295 |
f.Machine = Machine(hdr.Machine) |
| 296 |
f.Entry = uint64(hdr.Entry) |
| 297 |
if v := Version(hdr.Version); v != f.Version { |
| 298 |
return nil, &FormatError{0, "mismatched ELF version", v} |
| 299 |
} |
| 300 |
phoff = int64(hdr.Phoff) |
| 301 |
phentsize = int(hdr.Phentsize) |
| 302 |
phnum = int(hdr.Phnum) |
| 303 |
shoff = int64(hdr.Shoff) |
| 304 |
shentsize = int(hdr.Shentsize) |
| 305 |
shnum = int(hdr.Shnum) |
| 306 |
shstrndx = int(hdr.Shstrndx) |
| 307 |
case ELFCLASS64: |
| 308 |
hdr := new(Header64) |
| 309 |
sr.Seek(0, seekStart) |
| 310 |
if err := binary.Read(sr, f.ByteOrder, hdr); err != nil { |
| 311 |
return nil, err |
| 312 |
} |
| 313 |
f.Type = Type(hdr.Type) |
| 314 |
f.Machine = Machine(hdr.Machine) |
| 315 |
f.Entry = hdr.Entry |
| 316 |
if v := Version(hdr.Version); v != f.Version { |
| 317 |
return nil, &FormatError{0, "mismatched ELF version", v} |
| 318 |
} |
| 319 |
phoff = int64(hdr.Phoff) |
| 320 |
phentsize = int(hdr.Phentsize) |
| 321 |
phnum = int(hdr.Phnum) |
| 322 |
shoff = int64(hdr.Shoff) |
| 323 |
shentsize = int(hdr.Shentsize) |
| 324 |
shnum = int(hdr.Shnum) |
| 325 |
shstrndx = int(hdr.Shstrndx) |
| 326 |
} |
| 327 |
|
| 328 |
if shoff == 0 && shnum != 0 { |
| 329 |
return nil, &FormatError{0, "invalid ELF shnum for shoff=0", shnum} |
| 330 |
} |
| 331 |
|
| 332 |
if shnum > 0 && shstrndx >= shnum { |
| 333 |
return nil, &FormatError{0, "invalid ELF shstrndx", shstrndx} |
| 334 |
} |
| 335 |
|
| 336 |
// Read program headers |
| 337 |
f.Progs = make([]*Prog, phnum) |
| 338 |
for i := 0; i < phnum; i++ { |
| 339 |
off := phoff + int64(i)*int64(phentsize) |
| 340 |
sr.Seek(off, seekStart) |
| 341 |
p := new(Prog) |
| 342 |
switch f.Class { |
| 343 |
case ELFCLASS32: |
| 344 |
ph := new(Prog32) |
| 345 |
if err := binary.Read(sr, f.ByteOrder, ph); err != nil { |
| 346 |
return nil, err |
| 347 |
} |
| 348 |
p.ProgHeader = ProgHeader{ |
| 349 |
Type: ProgType(ph.Type), |
| 350 |
Flags: ProgFlag(ph.Flags), |
| 351 |
Off: uint64(ph.Off), |
| 352 |
Vaddr: uint64(ph.Vaddr), |
| 353 |
Paddr: uint64(ph.Paddr), |
| 354 |
Filesz: uint64(ph.Filesz), |
| 355 |
Memsz: uint64(ph.Memsz), |
| 356 |
Align: uint64(ph.Align), |
| 357 |
} |
| 358 |
case ELFCLASS64: |
| 359 |
ph := new(Prog64) |
| 360 |
if err := binary.Read(sr, f.ByteOrder, ph); err != nil { |
| 361 |
return nil, err |
| 362 |
} |
| 363 |
p.ProgHeader = ProgHeader{ |
| 364 |
Type: ProgType(ph.Type), |
| 365 |
Flags: ProgFlag(ph.Flags), |
| 366 |
Off: ph.Off, |
| 367 |
Vaddr: ph.Vaddr, |
| 368 |
Paddr: ph.Paddr, |
| 369 |
Filesz: ph.Filesz, |
| 370 |
Memsz: ph.Memsz, |
| 371 |
Align: ph.Align, |
| 372 |
} |
| 373 |
} |
| 374 |
p.sr = io.NewSectionReader(r, int64(p.Off), int64(p.Filesz)) |
| 375 |
p.ReaderAt = p.sr |
| 376 |
f.Progs[i] = p |
| 377 |
} |
| 378 |
|
| 379 |
// Read section headers |
| 380 |
f.Sections = make([]*Section, shnum) |
| 381 |
names := make([]uint32, shnum) |
| 382 |
for i := 0; i < shnum; i++ { |
| 383 |
off := shoff + int64(i)*int64(shentsize) |
| 384 |
sr.Seek(off, seekStart) |
| 385 |
s := new(Section) |
| 386 |
switch f.Class { |
| 387 |
case ELFCLASS32: |
| 388 |
sh := new(Section32) |
| 389 |
if err := binary.Read(sr, f.ByteOrder, sh); err != nil { |
| 390 |
return nil, err |
| 391 |
} |
| 392 |
names[i] = sh.Name |
| 393 |
s.SectionHeader = SectionHeader{ |
| 394 |
Type: SectionType(sh.Type), |
| 395 |
Flags: SectionFlag(sh.Flags), |
| 396 |
Addr: uint64(sh.Addr), |
| 397 |
Offset: uint64(sh.Off), |
| 398 |
FileSize: uint64(sh.Size), |
| 399 |
Link: sh.Link, |
| 400 |
Info: sh.Info, |
| 401 |
Addralign: uint64(sh.Addralign), |
| 402 |
Entsize: uint64(sh.Entsize), |
| 403 |
} |
| 404 |
case ELFCLASS64: |
| 405 |
sh := new(Section64) |
| 406 |
if err := binary.Read(sr, f.ByteOrder, sh); err != nil { |
| 407 |
return nil, err |
| 408 |
} |
| 409 |
names[i] = sh.Name |
| 410 |
s.SectionHeader = SectionHeader{ |
| 411 |
Type: SectionType(sh.Type), |
| 412 |
Flags: SectionFlag(sh.Flags), |
| 413 |
Offset: sh.Off, |
| 414 |
FileSize: sh.Size, |
| 415 |
Addr: sh.Addr, |
| 416 |
Link: sh.Link, |
| 417 |
Info: sh.Info, |
| 418 |
Addralign: sh.Addralign, |
| 419 |
Entsize: sh.Entsize, |
| 420 |
} |
| 421 |
} |
| 422 |
s.sr = io.NewSectionReader(r, int64(s.Offset), int64(s.FileSize)) |
| 423 |
|
| 424 |
if s.Flags&SHF_COMPRESSED == 0 { |
| 425 |
s.ReaderAt = s.sr |
| 426 |
s.Size = s.FileSize |
| 427 |
} else { |
| 428 |
// Read the compression header. |
| 429 |
switch f.Class { |
| 430 |
case ELFCLASS32: |
| 431 |
ch := new(Chdr32) |
| 432 |
if err := binary.Read(s.sr, f.ByteOrder, ch); err != nil { |
| 433 |
return nil, err |
| 434 |
} |
| 435 |
s.compressionType = CompressionType(ch.Type) |
| 436 |
s.Size = uint64(ch.Size) |
| 437 |
s.Addralign = uint64(ch.Addralign) |
| 438 |
s.compressionOffset = int64(binary.Size(ch)) |
| 439 |
case ELFCLASS64: |
| 440 |
ch := new(Chdr64) |
| 441 |
if err := binary.Read(s.sr, f.ByteOrder, ch); err != nil { |
| 442 |
return nil, err |
| 443 |
} |
| 444 |
s.compressionType = CompressionType(ch.Type) |
| 445 |
s.Size = ch.Size |
| 446 |
s.Addralign = ch.Addralign |
| 447 |
s.compressionOffset = int64(binary.Size(ch)) |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
f.Sections[i] = s |
| 452 |
} |
| 453 |
|
| 454 |
if len(f.Sections) == 0 { |
| 455 |
return f, nil |
| 456 |
} |
| 457 |
|
| 458 |
// Load section header string table. |
| 459 |
shstrtab, err := f.Sections[shstrndx].Data() |
| 460 |
if err != nil { |
| 461 |
return nil, err |
| 462 |
} |
| 463 |
for i, s := range f.Sections { |
| 464 |
var ok bool |
| 465 |
s.Name, ok = getString(shstrtab, int(names[i])) |
| 466 |
if !ok { |
| 467 |
return nil, &FormatError{shoff + int64(i*shentsize), "bad section name index", names[i]} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
return f, nil |
| 472 |
} |
| 473 |
|
| 474 |
// getSymbols returns a slice of Symbols from parsing the symbol table |
| 475 |
// with the given type, along with the associated string table. |
| 476 |
func (f *File) getSymbols(typ SectionType) ([]Symbol, []byte, error) { |
| 477 |
switch f.Class { |
| 478 |
case ELFCLASS64: |
| 479 |
return f.getSymbols64(typ) |
| 480 |
|
| 481 |
case ELFCLASS32: |
| 482 |
return f.getSymbols32(typ) |
| 483 |
} |
| 484 |
|
| 485 |
return nil, nil, errors.New("not implemented") |
| 486 |
} |
| 487 |
|
| 488 |
// ErrNoSymbols is returned by File.Symbols and File.DynamicSymbols |
| 489 |
// if there is no such section in the File. |
| 490 |
var ErrNoSymbols = errors.New("no symbol section") |
| 491 |
|
| 492 |
func (f *File) getSymbols32(typ SectionType) ([]Symbol, []byte, error) { |
| 493 |
symtabSection := f.SectionByType(typ) |
| 494 |
if symtabSection == nil { |
| 495 |
return nil, nil, ErrNoSymbols |
| 496 |
} |
| 497 |
|
| 498 |
data, err := symtabSection.Data() |
| 499 |
if err != nil { |
| 500 |
return nil, nil, fmt.Errorf("cannot load symbol section: %w", err) |
| 501 |
} |
| 502 |
symtab := bytes.NewReader(data) |
| 503 |
if symtab.Len()%Sym32Size != 0 { |
| 504 |
return nil, nil, errors.New("length of symbol section is not a multiple of SymSize") |
| 505 |
} |
| 506 |
|
| 507 |
strdata, err := f.stringTable(symtabSection.Link) |
| 508 |
if err != nil { |
| 509 |
return nil, nil, fmt.Errorf("cannot load string table section: %w", err) |
| 510 |
} |
| 511 |
|
| 512 |
// The first entry is all zeros. |
| 513 |
var skip [Sym32Size]byte |
| 514 |
symtab.Read(skip[:]) |
| 515 |
|
| 516 |
symbols := make([]Symbol, symtab.Len()/Sym32Size) |
| 517 |
|
| 518 |
i := 0 |
| 519 |
var sym Sym32 |
| 520 |
for symtab.Len() > 0 { |
| 521 |
binary.Read(symtab, f.ByteOrder, &sym) |
| 522 |
str, _ := getString(strdata, int(sym.Name)) |
| 523 |
symbols[i].Name = str |
| 524 |
symbols[i].Info = sym.Info |
| 525 |
symbols[i].Other = sym.Other |
| 526 |
symbols[i].Section = SectionIndex(sym.Shndx) |
| 527 |
symbols[i].Value = uint64(sym.Value) |
| 528 |
symbols[i].Size = uint64(sym.Size) |
| 529 |
i++ |
| 530 |
} |
| 531 |
|
| 532 |
return symbols, strdata, nil |
| 533 |
} |
| 534 |
|
| 535 |
func (f *File) getSymbols64(typ SectionType) ([]Symbol, []byte, error) { |
| 536 |
symtabSection := f.SectionByType(typ) |
| 537 |
if symtabSection == nil { |
| 538 |
return nil, nil, ErrNoSymbols |
| 539 |
} |
| 540 |
|
| 541 |
data, err := symtabSection.Data() |
| 542 |
if err != nil { |
| 543 |
return nil, nil, fmt.Errorf("cannot load symbol section: %w", err) |
| 544 |
} |
| 545 |
symtab := bytes.NewReader(data) |
| 546 |
if symtab.Len()%Sym64Size != 0 { |
| 547 |
return nil, nil, errors.New("length of symbol section is not a multiple of Sym64Size") |
| 548 |
} |
| 549 |
|
| 550 |
strdata, err := f.stringTable(symtabSection.Link) |
| 551 |
if err != nil { |
| 552 |
return nil, nil, fmt.Errorf("cannot load string table section: %w", err) |
| 553 |
} |
| 554 |
|
| 555 |
// The first entry is all zeros. |
| 556 |
var skip [Sym64Size]byte |
| 557 |
symtab.Read(skip[:]) |
| 558 |
|
| 559 |
symbols := make([]Symbol, symtab.Len()/Sym64Size) |
| 560 |
|
| 561 |
i := 0 |
| 562 |
var sym Sym64 |
| 563 |
for symtab.Len() > 0 { |
| 564 |
binary.Read(symtab, f.ByteOrder, &sym) |
| 565 |
str, _ := getString(strdata, int(sym.Name)) |
| 566 |
symbols[i].Name = str |
| 567 |
symbols[i].Info = sym.Info |
| 568 |
symbols[i].Other = sym.Other |
| 569 |
symbols[i].Section = SectionIndex(sym.Shndx) |
| 570 |
symbols[i].Value = sym.Value |
| 571 |
symbols[i].Size = sym.Size |
| 572 |
i++ |
| 573 |
} |
| 574 |
|
| 575 |
return symbols, strdata, nil |
| 576 |
} |
| 577 |
|
| 578 |
// getString extracts a string from an ELF string table. |
| 579 |
func getString(section []byte, start int) (string, bool) { |
| 580 |
if start < 0 || start >= len(section) { |
| 581 |
return "", false |
| 582 |
} |
| 583 |
|
| 584 |
for end := start; end < len(section); end++ { |
| 585 |
if section[end] == 0 { |
| 586 |
return string(section[start:end]), true |
| 587 |
} |
| 588 |
} |
| 589 |
return "", false |
| 590 |
} |
| 591 |
|
| 592 |
// Section returns a section with the given name, or nil if no such |
| 593 |
// section exists. |
| 594 |
func (f *File) Section(name string) *Section { |
| 595 |
for _, s := range f.Sections { |
| 596 |
if s.Name == name { |
| 597 |
return s |
| 598 |
} |
| 599 |
} |
| 600 |
return nil |
| 601 |
} |
| 602 |
|
| 603 |
// applyRelocations applies relocations to dst. rels is a relocations section |
| 604 |
// in REL or RELA format. |
| 605 |
func (f *File) applyRelocations(dst []byte, rels []byte) error { |
| 606 |
switch { |
| 607 |
case f.Class == ELFCLASS64 && f.Machine == EM_X86_64: |
| 608 |
return f.applyRelocationsAMD64(dst, rels) |
| 609 |
case f.Class == ELFCLASS32 && f.Machine == EM_386: |
| 610 |
return f.applyRelocations386(dst, rels) |
| 611 |
case f.Class == ELFCLASS32 && f.Machine == EM_ARM: |
| 612 |
return f.applyRelocationsARM(dst, rels) |
| 613 |
case f.Class == ELFCLASS64 && f.Machine == EM_AARCH64: |
| 614 |
return f.applyRelocationsARM64(dst, rels) |
| 615 |
case f.Class == ELFCLASS32 && f.Machine == EM_PPC: |
| 616 |
return f.applyRelocationsPPC(dst, rels) |
| 617 |
case f.Class == ELFCLASS64 && f.Machine == EM_PPC64: |
| 618 |
return f.applyRelocationsPPC64(dst, rels) |
| 619 |
case f.Class == ELFCLASS32 && f.Machine == EM_MIPS: |
| 620 |
return f.applyRelocationsMIPS(dst, rels) |
| 621 |
case f.Class == ELFCLASS64 && f.Machine == EM_MIPS: |
| 622 |
return f.applyRelocationsMIPS64(dst, rels) |
| 623 |
case f.Class == ELFCLASS32 && f.Machine == EM_RISCV: |
| 624 |
return f.applyRelocationsRISCV(dst, rels) |
| 625 |
case f.Class == ELFCLASS64 && f.Machine == EM_RISCV: |
| 626 |
return f.applyRelocationsRISCV64(dst, rels) |
| 627 |
case f.Class == ELFCLASS64 && f.Machine == EM_S390: |
| 628 |
return f.applyRelocationss390x(dst, rels) |
| 629 |
case f.Class == ELFCLASS32 && (f.Machine == EM_SPARC || f.Machine == EM_SPARC32PLUS): |
| 630 |
return f.applyRelocationsSPARC(dst, rels) |
| 631 |
case f.Class == ELFCLASS64 && f.Machine == EM_SPARCV9: |
| 632 |
return f.applyRelocationsSPARC64(dst, rels) |
| 633 |
case f.Class == ELFCLASS64 && f.Machine == EM_ALPHA: |
| 634 |
return f.applyRelocationsALPHA(dst, rels) |
| 635 |
default: |
| 636 |
return errors.New("applyRelocations: not implemented") |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
// canApplyRelocation reports whether we should try to apply a |
| 641 |
// relocation to a DWARF data section, given a pointer to the symbol |
| 642 |
// targeted by the relocation. |
| 643 |
// Most relocations in DWARF data tend to be section-relative, but |
| 644 |
// some target non-section symbols (for example, low_PC attrs on |
| 645 |
// subprogram or compilation unit DIEs that target function symbols). |
| 646 |
func canApplyRelocation(sym *Symbol) bool { |
| 647 |
return sym.Section != SHN_UNDEF && sym.Section < SHN_LORESERVE |
| 648 |
} |
| 649 |
|
| 650 |
func (f *File) applyRelocationsAMD64(dst []byte, rels []byte) error { |
| 651 |
// 24 is the size of Rela64. |
| 652 |
if len(rels)%24 != 0 { |
| 653 |
return errors.New("length of relocation section is not a multiple of 24") |
| 654 |
} |
| 655 |
|
| 656 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 657 |
if err != nil { |
| 658 |
return err |
| 659 |
} |
| 660 |
|
| 661 |
b := bytes.NewReader(rels) |
| 662 |
var rela Rela64 |
| 663 |
|
| 664 |
for b.Len() > 0 { |
| 665 |
binary.Read(b, f.ByteOrder, &rela) |
| 666 |
symNo := rela.Info >> 32 |
| 667 |
t := R_X86_64(rela.Info & 0xffff) |
| 668 |
|
| 669 |
if symNo == 0 || symNo > uint64(len(symbols)) { |
| 670 |
continue |
| 671 |
} |
| 672 |
sym := &symbols[symNo-1] |
| 673 |
if !canApplyRelocation(sym) { |
| 674 |
continue |
| 675 |
} |
| 676 |
|
| 677 |
// There are relocations, so this must be a normal |
| 678 |
// object file. The code below handles only basic relocations |
| 679 |
// of the form S + A (symbol plus addend). |
| 680 |
|
| 681 |
switch t { |
| 682 |
case R_X86_64_64: |
| 683 |
if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { |
| 684 |
continue |
| 685 |
} |
| 686 |
val64 := sym.Value + uint64(rela.Addend) |
| 687 |
f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], val64) |
| 688 |
case R_X86_64_32: |
| 689 |
if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { |
| 690 |
continue |
| 691 |
} |
| 692 |
val32 := uint32(sym.Value) + uint32(rela.Addend) |
| 693 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32) |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
return nil |
| 698 |
} |
| 699 |
|
| 700 |
func (f *File) applyRelocations386(dst []byte, rels []byte) error { |
| 701 |
// 8 is the size of Rel32. |
| 702 |
if len(rels)%8 != 0 { |
| 703 |
return errors.New("length of relocation section is not a multiple of 8") |
| 704 |
} |
| 705 |
|
| 706 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 707 |
if err != nil { |
| 708 |
return err |
| 709 |
} |
| 710 |
|
| 711 |
b := bytes.NewReader(rels) |
| 712 |
var rel Rel32 |
| 713 |
|
| 714 |
for b.Len() > 0 { |
| 715 |
binary.Read(b, f.ByteOrder, &rel) |
| 716 |
symNo := rel.Info >> 8 |
| 717 |
t := R_386(rel.Info & 0xff) |
| 718 |
|
| 719 |
if symNo == 0 || symNo > uint32(len(symbols)) { |
| 720 |
continue |
| 721 |
} |
| 722 |
sym := &symbols[symNo-1] |
| 723 |
|
| 724 |
if t == R_386_32 { |
| 725 |
if rel.Off+4 >= uint32(len(dst)) { |
| 726 |
continue |
| 727 |
} |
| 728 |
val := f.ByteOrder.Uint32(dst[rel.Off : rel.Off+4]) |
| 729 |
val += uint32(sym.Value) |
| 730 |
f.ByteOrder.PutUint32(dst[rel.Off:rel.Off+4], val) |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
return nil |
| 735 |
} |
| 736 |
|
| 737 |
func (f *File) applyRelocationsARM(dst []byte, rels []byte) error { |
| 738 |
// 8 is the size of Rel32. |
| 739 |
if len(rels)%8 != 0 { |
| 740 |
return errors.New("length of relocation section is not a multiple of 8") |
| 741 |
} |
| 742 |
|
| 743 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 744 |
if err != nil { |
| 745 |
return err |
| 746 |
} |
| 747 |
|
| 748 |
b := bytes.NewReader(rels) |
| 749 |
var rel Rel32 |
| 750 |
|
| 751 |
for b.Len() > 0 { |
| 752 |
binary.Read(b, f.ByteOrder, &rel) |
| 753 |
symNo := rel.Info >> 8 |
| 754 |
t := R_ARM(rel.Info & 0xff) |
| 755 |
|
| 756 |
if symNo == 0 || symNo > uint32(len(symbols)) { |
| 757 |
continue |
| 758 |
} |
| 759 |
sym := &symbols[symNo-1] |
| 760 |
|
| 761 |
switch t { |
| 762 |
case R_ARM_ABS32: |
| 763 |
if rel.Off+4 >= uint32(len(dst)) { |
| 764 |
continue |
| 765 |
} |
| 766 |
val := f.ByteOrder.Uint32(dst[rel.Off : rel.Off+4]) |
| 767 |
val += uint32(sym.Value) |
| 768 |
f.ByteOrder.PutUint32(dst[rel.Off:rel.Off+4], val) |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
return nil |
| 773 |
} |
| 774 |
|
| 775 |
func (f *File) applyRelocationsARM64(dst []byte, rels []byte) error { |
| 776 |
// 24 is the size of Rela64. |
| 777 |
if len(rels)%24 != 0 { |
| 778 |
return errors.New("length of relocation section is not a multiple of 24") |
| 779 |
} |
| 780 |
|
| 781 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 782 |
if err != nil { |
| 783 |
return err |
| 784 |
} |
| 785 |
|
| 786 |
b := bytes.NewReader(rels) |
| 787 |
var rela Rela64 |
| 788 |
|
| 789 |
for b.Len() > 0 { |
| 790 |
binary.Read(b, f.ByteOrder, &rela) |
| 791 |
symNo := rela.Info >> 32 |
| 792 |
t := R_AARCH64(rela.Info & 0xffff) |
| 793 |
|
| 794 |
if symNo == 0 || symNo > uint64(len(symbols)) { |
| 795 |
continue |
| 796 |
} |
| 797 |
sym := &symbols[symNo-1] |
| 798 |
if !canApplyRelocation(sym) { |
| 799 |
continue |
| 800 |
} |
| 801 |
|
| 802 |
// There are relocations, so this must be a normal |
| 803 |
// object file. The code below handles only basic relocations |
| 804 |
// of the form S + A (symbol plus addend). |
| 805 |
|
| 806 |
switch t { |
| 807 |
case R_AARCH64_ABS64: |
| 808 |
if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { |
| 809 |
continue |
| 810 |
} |
| 811 |
val64 := sym.Value + uint64(rela.Addend) |
| 812 |
f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], val64) |
| 813 |
case R_AARCH64_ABS32: |
| 814 |
if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { |
| 815 |
continue |
| 816 |
} |
| 817 |
val32 := uint32(sym.Value) + uint32(rela.Addend) |
| 818 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32) |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
return nil |
| 823 |
} |
| 824 |
|
| 825 |
func (f *File) applyRelocationsPPC(dst []byte, rels []byte) error { |
| 826 |
// 12 is the size of Rela32. |
| 827 |
if len(rels)%12 != 0 { |
| 828 |
return errors.New("length of relocation section is not a multiple of 12") |
| 829 |
} |
| 830 |
|
| 831 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 832 |
if err != nil { |
| 833 |
return err |
| 834 |
} |
| 835 |
|
| 836 |
b := bytes.NewReader(rels) |
| 837 |
var rela Rela32 |
| 838 |
|
| 839 |
for b.Len() > 0 { |
| 840 |
binary.Read(b, f.ByteOrder, &rela) |
| 841 |
symNo := rela.Info >> 8 |
| 842 |
t := R_PPC(rela.Info & 0xff) |
| 843 |
|
| 844 |
if symNo == 0 || symNo > uint32(len(symbols)) { |
| 845 |
continue |
| 846 |
} |
| 847 |
sym := &symbols[symNo-1] |
| 848 |
if !canApplyRelocation(sym) { |
| 849 |
continue |
| 850 |
} |
| 851 |
|
| 852 |
switch t { |
| 853 |
case R_PPC_ADDR32: |
| 854 |
if rela.Off+4 >= uint32(len(dst)) || rela.Addend < 0 { |
| 855 |
continue |
| 856 |
} |
| 857 |
val32 := uint32(sym.Value) + uint32(rela.Addend) |
| 858 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32) |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
return nil |
| 863 |
} |
| 864 |
|
| 865 |
func (f *File) applyRelocationsPPC64(dst []byte, rels []byte) error { |
| 866 |
// 24 is the size of Rela64. |
| 867 |
if len(rels)%24 != 0 { |
| 868 |
return errors.New("length of relocation section is not a multiple of 24") |
| 869 |
} |
| 870 |
|
| 871 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 872 |
if err != nil { |
| 873 |
return err |
| 874 |
} |
| 875 |
|
| 876 |
b := bytes.NewReader(rels) |
| 877 |
var rela Rela64 |
| 878 |
|
| 879 |
for b.Len() > 0 { |
| 880 |
binary.Read(b, f.ByteOrder, &rela) |
| 881 |
symNo := rela.Info >> 32 |
| 882 |
t := R_PPC64(rela.Info & 0xffff) |
| 883 |
|
| 884 |
if symNo == 0 || symNo > uint64(len(symbols)) { |
| 885 |
continue |
| 886 |
} |
| 887 |
sym := &symbols[symNo-1] |
| 888 |
if !canApplyRelocation(sym) { |
| 889 |
continue |
| 890 |
} |
| 891 |
|
| 892 |
switch t { |
| 893 |
case R_PPC64_ADDR64: |
| 894 |
if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { |
| 895 |
continue |
| 896 |
} |
| 897 |
val64 := sym.Value + uint64(rela.Addend) |
| 898 |
f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], val64) |
| 899 |
case R_PPC64_ADDR32: |
| 900 |
if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { |
| 901 |
continue |
| 902 |
} |
| 903 |
val32 := uint32(sym.Value) + uint32(rela.Addend) |
| 904 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32) |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
return nil |
| 909 |
} |
| 910 |
|
| 911 |
func (f *File) applyRelocationsMIPS(dst []byte, rels []byte) error { |
| 912 |
// 8 is the size of Rel32. |
| 913 |
if len(rels)%8 != 0 { |
| 914 |
return errors.New("length of relocation section is not a multiple of 8") |
| 915 |
} |
| 916 |
|
| 917 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 918 |
if err != nil { |
| 919 |
return err |
| 920 |
} |
| 921 |
|
| 922 |
b := bytes.NewReader(rels) |
| 923 |
var rel Rel32 |
| 924 |
|
| 925 |
for b.Len() > 0 { |
| 926 |
binary.Read(b, f.ByteOrder, &rel) |
| 927 |
symNo := rel.Info >> 8 |
| 928 |
t := R_MIPS(rel.Info & 0xff) |
| 929 |
|
| 930 |
if symNo == 0 || symNo > uint32(len(symbols)) { |
| 931 |
continue |
| 932 |
} |
| 933 |
sym := &symbols[symNo-1] |
| 934 |
|
| 935 |
switch t { |
| 936 |
case R_MIPS_32: |
| 937 |
if rel.Off+4 >= uint32(len(dst)) { |
| 938 |
continue |
| 939 |
} |
| 940 |
val := f.ByteOrder.Uint32(dst[rel.Off : rel.Off+4]) |
| 941 |
val += uint32(sym.Value) |
| 942 |
f.ByteOrder.PutUint32(dst[rel.Off:rel.Off+4], val) |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
return nil |
| 947 |
} |
| 948 |
|
| 949 |
func (f *File) applyRelocationsMIPS64(dst []byte, rels []byte) error { |
| 950 |
// 24 is the size of Rela64. |
| 951 |
if len(rels)%24 != 0 { |
| 952 |
return errors.New("length of relocation section is not a multiple of 24") |
| 953 |
} |
| 954 |
|
| 955 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 956 |
if err != nil { |
| 957 |
return err |
| 958 |
} |
| 959 |
|
| 960 |
b := bytes.NewReader(rels) |
| 961 |
var rela Rela64 |
| 962 |
|
| 963 |
for b.Len() > 0 { |
| 964 |
binary.Read(b, f.ByteOrder, &rela) |
| 965 |
var symNo uint64 |
| 966 |
var t R_MIPS |
| 967 |
if f.ByteOrder == binary.BigEndian { |
| 968 |
symNo = rela.Info >> 32 |
| 969 |
t = R_MIPS(rela.Info & 0xff) |
| 970 |
} else { |
| 971 |
symNo = rela.Info & 0xffffffff |
| 972 |
t = R_MIPS(rela.Info >> 56) |
| 973 |
} |
| 974 |
|
| 975 |
if symNo == 0 || symNo > uint64(len(symbols)) { |
| 976 |
continue |
| 977 |
} |
| 978 |
sym := &symbols[symNo-1] |
| 979 |
if !canApplyRelocation(sym) { |
| 980 |
continue |
| 981 |
} |
| 982 |
|
| 983 |
switch t { |
| 984 |
case R_MIPS_64: |
| 985 |
if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { |
| 986 |
continue |
| 987 |
} |
| 988 |
val64 := sym.Value + uint64(rela.Addend) |
| 989 |
f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], val64) |
| 990 |
case R_MIPS_32: |
| 991 |
if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { |
| 992 |
continue |
| 993 |
} |
| 994 |
val32 := uint32(sym.Value) + uint32(rela.Addend) |
| 995 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32) |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
return nil |
| 1000 |
} |
| 1001 |
|
| 1002 |
func (f *File) applyRelocationsRISCV(dst []byte, rels []byte) error { |
| 1003 |
// 12 is the size of Rela32. |
| 1004 |
if len(rels)%12 != 0 { |
| 1005 |
return errors.New("length of relocation section is not a multiple of 12") |
| 1006 |
} |
| 1007 |
|
| 1008 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 1009 |
if err != nil { |
| 1010 |
return err |
| 1011 |
} |
| 1012 |
|
| 1013 |
b := bytes.NewReader(rels) |
| 1014 |
var rela Rela32 |
| 1015 |
|
| 1016 |
for b.Len() > 0 { |
| 1017 |
binary.Read(b, f.ByteOrder, &rela) |
| 1018 |
symNo := rela.Info >> 8 |
| 1019 |
t := R_RISCV(rela.Info & 0xff) |
| 1020 |
|
| 1021 |
if symNo == 0 || symNo > uint32(len(symbols)) { |
| 1022 |
continue |
| 1023 |
} |
| 1024 |
sym := &symbols[symNo-1] |
| 1025 |
if !canApplyRelocation(sym) { |
| 1026 |
continue |
| 1027 |
} |
| 1028 |
|
| 1029 |
switch t { |
| 1030 |
case R_RISCV_32: |
| 1031 |
if rela.Off+4 >= uint32(len(dst)) || rela.Addend < 0 { |
| 1032 |
continue |
| 1033 |
} |
| 1034 |
val32 := uint32(sym.Value) + uint32(rela.Addend) |
| 1035 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32) |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
return nil |
| 1040 |
} |
| 1041 |
|
| 1042 |
func (f *File) applyRelocationsRISCV64(dst []byte, rels []byte) error { |
| 1043 |
// 24 is the size of Rela64. |
| 1044 |
if len(rels)%24 != 0 { |
| 1045 |
return errors.New("length of relocation section is not a multiple of 24") |
| 1046 |
} |
| 1047 |
|
| 1048 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 1049 |
if err != nil { |
| 1050 |
return err |
| 1051 |
} |
| 1052 |
|
| 1053 |
b := bytes.NewReader(rels) |
| 1054 |
var rela Rela64 |
| 1055 |
|
| 1056 |
for b.Len() > 0 { |
| 1057 |
binary.Read(b, f.ByteOrder, &rela) |
| 1058 |
symNo := rela.Info >> 32 |
| 1059 |
t := R_RISCV(rela.Info & 0xffff) |
| 1060 |
|
| 1061 |
if symNo == 0 || symNo > uint64(len(symbols)) { |
| 1062 |
continue |
| 1063 |
} |
| 1064 |
sym := &symbols[symNo-1] |
| 1065 |
if !canApplyRelocation(sym) { |
| 1066 |
continue |
| 1067 |
} |
| 1068 |
|
| 1069 |
switch t { |
| 1070 |
case R_RISCV_64: |
| 1071 |
if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { |
| 1072 |
continue |
| 1073 |
} |
| 1074 |
val64 := sym.Value + uint64(rela.Addend) |
| 1075 |
f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], val64) |
| 1076 |
case R_RISCV_32: |
| 1077 |
if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { |
| 1078 |
continue |
| 1079 |
} |
| 1080 |
val32 := uint32(sym.Value) + uint32(rela.Addend) |
| 1081 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32) |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
return nil |
| 1086 |
} |
| 1087 |
|
| 1088 |
func (f *File) applyRelocationss390x(dst []byte, rels []byte) error { |
| 1089 |
// 24 is the size of Rela64. |
| 1090 |
if len(rels)%24 != 0 { |
| 1091 |
return errors.New("length of relocation section is not a multiple of 24") |
| 1092 |
} |
| 1093 |
|
| 1094 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 1095 |
if err != nil { |
| 1096 |
return err |
| 1097 |
} |
| 1098 |
|
| 1099 |
b := bytes.NewReader(rels) |
| 1100 |
var rela Rela64 |
| 1101 |
|
| 1102 |
for b.Len() > 0 { |
| 1103 |
binary.Read(b, f.ByteOrder, &rela) |
| 1104 |
symNo := rela.Info >> 32 |
| 1105 |
t := R_390(rela.Info & 0xffff) |
| 1106 |
|
| 1107 |
if symNo == 0 || symNo > uint64(len(symbols)) { |
| 1108 |
continue |
| 1109 |
} |
| 1110 |
sym := &symbols[symNo-1] |
| 1111 |
if !canApplyRelocation(sym) { |
| 1112 |
continue |
| 1113 |
} |
| 1114 |
|
| 1115 |
switch t { |
| 1116 |
case R_390_64: |
| 1117 |
if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { |
| 1118 |
continue |
| 1119 |
} |
| 1120 |
val64 := sym.Value + uint64(rela.Addend) |
| 1121 |
f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], val64) |
| 1122 |
case R_390_32: |
| 1123 |
if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { |
| 1124 |
continue |
| 1125 |
} |
| 1126 |
val32 := uint32(sym.Value) + uint32(rela.Addend) |
| 1127 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32) |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
return nil |
| 1132 |
} |
| 1133 |
|
| 1134 |
func (f *File) applyRelocationsSPARC(dst []byte, rels []byte) error { |
| 1135 |
// 12 is the size of Rela32. |
| 1136 |
if len(rels)%12 != 0 { |
| 1137 |
return errors.New("length of relocation section is not a multiple of 12") |
| 1138 |
} |
| 1139 |
|
| 1140 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 1141 |
if err != nil { |
| 1142 |
return err |
| 1143 |
} |
| 1144 |
|
| 1145 |
b := bytes.NewReader(rels) |
| 1146 |
var rela Rela32 |
| 1147 |
|
| 1148 |
for b.Len() > 0 { |
| 1149 |
binary.Read(b, f.ByteOrder, &rela) |
| 1150 |
symNo := rela.Info >> 32 |
| 1151 |
t := R_SPARC(rela.Info & 0xff) |
| 1152 |
|
| 1153 |
if symNo == 0 || symNo > uint32(len(symbols)) { |
| 1154 |
continue |
| 1155 |
} |
| 1156 |
sym := &symbols[symNo-1] |
| 1157 |
if SymType(sym.Info&0xf) != STT_SECTION { |
| 1158 |
// We don't handle non-section relocations for now. |
| 1159 |
continue |
| 1160 |
} |
| 1161 |
|
| 1162 |
switch t { |
| 1163 |
case R_SPARC_32, R_SPARC_UA32: |
| 1164 |
if rela.Off+4 >= uint32(len(dst)) || rela.Addend < 0 { |
| 1165 |
continue |
| 1166 |
} |
| 1167 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], uint32(rela.Addend)) |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
return nil |
| 1172 |
} |
| 1173 |
|
| 1174 |
func (f *File) applyRelocationsSPARC64(dst []byte, rels []byte) error { |
| 1175 |
// 24 is the size of Rela64. |
| 1176 |
if len(rels)%24 != 0 { |
| 1177 |
return errors.New("length of relocation section is not a multiple of 24") |
| 1178 |
} |
| 1179 |
|
| 1180 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 1181 |
if err != nil { |
| 1182 |
return err |
| 1183 |
} |
| 1184 |
|
| 1185 |
b := bytes.NewReader(rels) |
| 1186 |
var rela Rela64 |
| 1187 |
|
| 1188 |
for b.Len() > 0 { |
| 1189 |
binary.Read(b, f.ByteOrder, &rela) |
| 1190 |
symNo := rela.Info >> 32 |
| 1191 |
t := R_SPARC(rela.Info & 0xff) |
| 1192 |
|
| 1193 |
if symNo == 0 || symNo > uint64(len(symbols)) { |
| 1194 |
continue |
| 1195 |
} |
| 1196 |
sym := &symbols[symNo-1] |
| 1197 |
if !canApplyRelocation(sym) { |
| 1198 |
continue |
| 1199 |
} |
| 1200 |
|
| 1201 |
switch t { |
| 1202 |
case R_SPARC_64, R_SPARC_UA64: |
| 1203 |
if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { |
| 1204 |
continue |
| 1205 |
} |
| 1206 |
val64 := sym.Value + uint64(rela.Addend) |
| 1207 |
f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], val64) |
| 1208 |
case R_SPARC_32, R_SPARC_UA32: |
| 1209 |
if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { |
| 1210 |
continue |
| 1211 |
} |
| 1212 |
val32 := uint32(sym.Value) + uint32(rela.Addend) |
| 1213 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32) |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
return nil |
| 1218 |
} |
| 1219 |
|
| 1220 |
func (f *File) applyRelocationsALPHA(dst []byte, rels []byte) error { |
| 1221 |
// 24 is the size of Rela64. |
| 1222 |
if len(rels)%24 != 0 { |
| 1223 |
return errors.New("length of relocation section is not a multiple of 24") |
| 1224 |
} |
| 1225 |
|
| 1226 |
symbols, _, err := f.getSymbols(SHT_SYMTAB) |
| 1227 |
if err != nil { |
| 1228 |
return err |
| 1229 |
} |
| 1230 |
|
| 1231 |
b := bytes.NewReader(rels) |
| 1232 |
var rela Rela64 |
| 1233 |
|
| 1234 |
for b.Len() > 0 { |
| 1235 |
binary.Read(b, f.ByteOrder, &rela) |
| 1236 |
symNo := rela.Info >> 32 |
| 1237 |
t := R_ALPHA(rela.Info & 0xffff) |
| 1238 |
|
| 1239 |
if symNo == 0 || symNo > uint64(len(symbols)) { |
| 1240 |
continue |
| 1241 |
} |
| 1242 |
sym := &symbols[symNo-1] |
| 1243 |
if SymType(sym.Info&0xf) != STT_SECTION { |
| 1244 |
// We don't handle non-section relocations for now. |
| 1245 |
continue |
| 1246 |
} |
| 1247 |
|
| 1248 |
// There are relocations, so this must be a normal |
| 1249 |
// object file, and we only look at section symbols, |
| 1250 |
// so we assume that the symbol value is 0. |
| 1251 |
|
| 1252 |
switch t { |
| 1253 |
case R_ALPHA_REFQUAD: |
| 1254 |
if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { |
| 1255 |
continue |
| 1256 |
} |
| 1257 |
f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], uint64(rela.Addend)) |
| 1258 |
case R_ALPHA_REFLONG: |
| 1259 |
if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { |
| 1260 |
continue |
| 1261 |
} |
| 1262 |
f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], uint32(rela.Addend)) |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
return nil |
| 1267 |
} |
| 1268 |
|
| 1269 |
func (f *File) DWARF() (*dwarf.Data, error) { |
| 1270 |
dwarfSuffix := func(s *Section) string { |
| 1271 |
switch { |
| 1272 |
case strings.HasPrefix(s.Name, ".debug_"): |
| 1273 |
return s.Name[7:] |
| 1274 |
case strings.HasPrefix(s.Name, ".zdebug_"): |
| 1275 |
return s.Name[8:] |
| 1276 |
default: |
| 1277 |
return "" |
| 1278 |
} |
| 1279 |
|
| 1280 |
} |
| 1281 |
// sectionData gets the data for s, checks its size, and |
| 1282 |
// applies any applicable relations. |
| 1283 |
sectionData := func(i int, s *Section) ([]byte, error) { |
| 1284 |
b, err := s.Data() |
| 1285 |
if err != nil && uint64(len(b)) < s.Size { |
| 1286 |
return nil, err |
| 1287 |
} |
| 1288 |
|
| 1289 |
if len(b) >= 12 && string(b[:4]) == "ZLIB" { |
| 1290 |
dlen := binary.BigEndian.Uint64(b[4:12]) |
| 1291 |
dbuf := make([]byte, dlen) |
| 1292 |
r, err := zlib.NewReader(bytes.NewBuffer(b[12:])) |
| 1293 |
if err != nil { |
| 1294 |
return nil, err |
| 1295 |
} |
| 1296 |
if _, err := io.ReadFull(r, dbuf); err != nil { |
| 1297 |
return nil, err |
| 1298 |
} |
| 1299 |
if err := r.Close(); err != nil { |
| 1300 |
return nil, err |
| 1301 |
} |
| 1302 |
b = dbuf |
| 1303 |
} |
| 1304 |
|
| 1305 |
if f.Type == ET_EXEC { |
| 1306 |
// Do not apply relocations to DWARF sections for ET_EXEC binaries. |
| 1307 |
// Relocations should already be applied, and .rela sections may |
| 1308 |
// contain incorrect data. |
| 1309 |
return b, nil |
| 1310 |
} |
| 1311 |
|
| 1312 |
for _, r := range f.Sections { |
| 1313 |
if r.Type != SHT_RELA && r.Type != SHT_REL { |
| 1314 |
continue |
| 1315 |
} |
| 1316 |
if int(r.Info) != i { |
| 1317 |
continue |
| 1318 |
} |
| 1319 |
rd, err := r.Data() |
| 1320 |
if err != nil { |
| 1321 |
return nil, err |
| 1322 |
} |
| 1323 |
err = f.applyRelocations(b, rd) |
| 1324 |
if err != nil { |
| 1325 |
return nil, err |
| 1326 |
} |
| 1327 |
} |
| 1328 |
return b, nil |
| 1329 |
} |
| 1330 |
|
| 1331 |
// There are many DWARf sections, but these are the ones |
| 1332 |
// the debug/dwarf package started with. |
| 1333 |
var dat = map[string][]byte{"abbrev": nil, "info": nil, "str": nil, "line": nil, "ranges": nil} |
| 1334 |
for i, s := range f.Sections { |
| 1335 |
suffix := dwarfSuffix(s) |
| 1336 |
if suffix == "" { |
| 1337 |
continue |
| 1338 |
} |
| 1339 |
if _, ok := dat[suffix]; !ok { |
| 1340 |
continue |
| 1341 |
} |
| 1342 |
b, err := sectionData(i, s) |
| 1343 |
if err != nil { |
| 1344 |
return nil, err |
| 1345 |
} |
| 1346 |
dat[suffix] = b |
| 1347 |
} |
| 1348 |
|
| 1349 |
d, err := dwarf.New(dat["abbrev"], nil, nil, dat["info"], dat["line"], nil, dat["ranges"], dat["str"]) |
| 1350 |
if err != nil { |
| 1351 |
return nil, err |
| 1352 |
} |
| 1353 |
|
| 1354 |
// Look for DWARF4 .debug_types sections and DWARF5 sections. |
| 1355 |
for i, s := range f.Sections { |
| 1356 |
suffix := dwarfSuffix(s) |
| 1357 |
if suffix == "" { |
| 1358 |
continue |
| 1359 |
} |
| 1360 |
if _, ok := dat[suffix]; ok { |
| 1361 |
// Already handled. |
| 1362 |
continue |
| 1363 |
} |
| 1364 |
|
| 1365 |
b, err := sectionData(i, s) |
| 1366 |
if err != nil { |
| 1367 |
return nil, err |
| 1368 |
} |
| 1369 |
|
| 1370 |
if suffix == "types" { |
| 1371 |
if err := d.AddTypes(fmt.Sprintf("types-%d", i), b); err != nil { |
| 1372 |
return nil, err |
| 1373 |
} |
| 1374 |
} else { |
| 1375 |
if err := d.AddSection(".debug_"+suffix, b); err != nil { |
| 1376 |
return nil, err |
| 1377 |
} |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
return d, nil |
| 1382 |
} |
| 1383 |
|
| 1384 |
// Symbols returns the symbol table for f. The symbols will be listed in the order |
| 1385 |
// they appear in f. |
| 1386 |
// |
| 1387 |
// For compatibility with Go 1.0, Symbols omits the null symbol at index 0. |
| 1388 |
// After retrieving the symbols as symtab, an externally supplied index x |
| 1389 |
// corresponds to symtab[x-1], not symtab[x]. |
| 1390 |
func (f *File) Symbols() ([]Symbol, error) { |
| 1391 |
sym, _, err := f.getSymbols(SHT_SYMTAB) |
| 1392 |
return sym, err |
| 1393 |
} |
| 1394 |
|
| 1395 |
// DynamicSymbols returns the dynamic symbol table for f. The symbols |
| 1396 |
// will be listed in the order they appear in f. |
| 1397 |
// |
| 1398 |
// If f has a symbol version table, the returned Symbols will have |
| 1399 |
// initialized Version and Library fields. |
| 1400 |
// |
| 1401 |
// For compatibility with Symbols, DynamicSymbols omits the null symbol at index 0. |
| 1402 |
// After retrieving the symbols as symtab, an externally supplied index x |
| 1403 |
// corresponds to symtab[x-1], not symtab[x]. |
| 1404 |
func (f *File) DynamicSymbols() ([]Symbol, error) { |
| 1405 |
sym, str, err := f.getSymbols(SHT_DYNSYM) |
| 1406 |
if err != nil { |
| 1407 |
return nil, err |
| 1408 |
} |
| 1409 |
if f.gnuVersionInit(str) { |
| 1410 |
for i := range sym { |
| 1411 |
sym[i].Library, sym[i].Version = f.gnuVersion(i) |
| 1412 |
} |
| 1413 |
} |
| 1414 |
return sym, nil |
| 1415 |
} |
| 1416 |
|
| 1417 |
type ImportedSymbol struct { |
| 1418 |
Name string |
| 1419 |
Version string |
| 1420 |
Library string |
| 1421 |
} |
| 1422 |
|
| 1423 |
// ImportedSymbols returns the names of all symbols |
| 1424 |
// referred to by the binary f that are expected to be |
| 1425 |
// satisfied by other libraries at dynamic load time. |
| 1426 |
// It does not return weak symbols. |
| 1427 |
func (f *File) ImportedSymbols() ([]ImportedSymbol, error) { |
| 1428 |
sym, str, err := f.getSymbols(SHT_DYNSYM) |
| 1429 |
if err != nil { |
| 1430 |
return nil, err |
| 1431 |
} |
| 1432 |
f.gnuVersionInit(str) |
| 1433 |
var all []ImportedSymbol |
| 1434 |
for i, s := range sym { |
| 1435 |
if ST_BIND(s.Info) == STB_GLOBAL && s.Section == SHN_UNDEF { |
| 1436 |
all = append(all, ImportedSymbol{Name: s.Name}) |
| 1437 |
sym := &all[len(all)-1] |
| 1438 |
sym.Library, sym.Version = f.gnuVersion(i) |
| 1439 |
} |
| 1440 |
} |
| 1441 |
return all, nil |
| 1442 |
} |
| 1443 |
|
| 1444 |
type verneed struct { |
| 1445 |
File string |
| 1446 |
Name string |
| 1447 |
} |
| 1448 |
|
| 1449 |
// gnuVersionInit parses the GNU version tables |
| 1450 |
// for use by calls to gnuVersion. |
| 1451 |
func (f *File) gnuVersionInit(str []byte) bool { |
| 1452 |
if f.gnuNeed != nil { |
| 1453 |
// Already initialized |
| 1454 |
return true |
| 1455 |
} |
| 1456 |
|
| 1457 |
// Accumulate verneed information. |
| 1458 |
vn := f.SectionByType(SHT_GNU_VERNEED) |
| 1459 |
if vn == nil { |
| 1460 |
return false |
| 1461 |
} |
| 1462 |
d, _ := vn.Data() |
| 1463 |
|
| 1464 |
var need []verneed |
| 1465 |
i := 0 |
| 1466 |
for { |
| 1467 |
if i+16 > len(d) { |
| 1468 |
break |
| 1469 |
} |
| 1470 |
vers := f.ByteOrder.Uint16(d[i : i+2]) |
| 1471 |
if vers != 1 { |
| 1472 |
break |
| 1473 |
} |
| 1474 |
cnt := f.ByteOrder.Uint16(d[i+2 : i+4]) |
| 1475 |
fileoff := f.ByteOrder.Uint32(d[i+4 : i+8]) |
| 1476 |
aux := f.ByteOrder.Uint32(d[i+8 : i+12]) |
| 1477 |
next := f.ByteOrder.Uint32(d[i+12 : i+16]) |
| 1478 |
file, _ := getString(str, int(fileoff)) |
| 1479 |
|
| 1480 |
var name string |
| 1481 |
j := i + int(aux) |
| 1482 |
for c := 0; c < int(cnt); c++ { |
| 1483 |
if j+16 > len(d) { |
| 1484 |
break |
| 1485 |
} |
| 1486 |
// hash := f.ByteOrder.Uint32(d[j:j+4]) |
| 1487 |
// flags := f.ByteOrder.Uint16(d[j+4:j+6]) |
| 1488 |
other := f.ByteOrder.Uint16(d[j+6 : j+8]) |
| 1489 |
nameoff := f.ByteOrder.Uint32(d[j+8 : j+12]) |
| 1490 |
next := f.ByteOrder.Uint32(d[j+12 : j+16]) |
| 1491 |
name, _ = getString(str, int(nameoff)) |
| 1492 |
ndx := int(other) |
| 1493 |
if ndx >= len(need) { |
| 1494 |
a := make([]verneed, 2*(ndx+1)) |
| 1495 |
copy(a, need) |
| 1496 |
need = a |
| 1497 |
} |
| 1498 |
|
| 1499 |
need[ndx] = verneed{file, name} |
| 1500 |
if next == 0 { |
| 1501 |
break |
| 1502 |
} |
| 1503 |
j += int(next) |
| 1504 |
} |
| 1505 |
|
| 1506 |
if next == 0 { |
| 1507 |
break |
| 1508 |
} |
| 1509 |
i += int(next) |
| 1510 |
} |
| 1511 |
|
| 1512 |
// Versym parallels symbol table, indexing into verneed. |
| 1513 |
vs := f.SectionByType(SHT_GNU_VERSYM) |
| 1514 |
if vs == nil { |
| 1515 |
return false |
| 1516 |
} |
| 1517 |
d, _ = vs.Data() |
| 1518 |
|
| 1519 |
f.gnuNeed = need |
| 1520 |
f.gnuVersym = d |
| 1521 |
return true |
| 1522 |
} |
| 1523 |
|
| 1524 |
// gnuVersion adds Library and Version information to sym, |
| 1525 |
// which came from offset i of the symbol table. |
| 1526 |
func (f *File) gnuVersion(i int) (library string, version string) { |
| 1527 |
// Each entry is two bytes. |
| 1528 |
i = (i + 1) * 2 |
| 1529 |
if i >= len(f.gnuVersym) { |
| 1530 |
return |
| 1531 |
} |
| 1532 |
j := int(f.ByteOrder.Uint16(f.gnuVersym[i:])) |
| 1533 |
if j < 2 || j >= len(f.gnuNeed) { |
| 1534 |
return |
| 1535 |
} |
| 1536 |
n := &f.gnuNeed[j] |
| 1537 |
return n.File, n.Name |
| 1538 |
} |
| 1539 |
|
| 1540 |
// ImportedLibraries returns the names of all libraries |
| 1541 |
// referred to by the binary f that are expected to be |
| 1542 |
// linked with the binary at dynamic link time. |
| 1543 |
func (f *File) ImportedLibraries() ([]string, error) { |
| 1544 |
return f.DynString(DT_NEEDED) |
| 1545 |
} |
| 1546 |
|
| 1547 |
// DynString returns the strings listed for the given tag in the file's dynamic |
| 1548 |
// section. |
| 1549 |
// |
| 1550 |
// The tag must be one that takes string values: DT_NEEDED, DT_SONAME, DT_RPATH, or |
| 1551 |
// DT_RUNPATH. |
| 1552 |
func (f *File) DynString(tag DynTag) ([]string, error) { |
| 1553 |
switch tag { |
| 1554 |
case DT_NEEDED, DT_SONAME, DT_RPATH, DT_RUNPATH: |
| 1555 |
default: |
| 1556 |
return nil, fmt.Errorf("non-string-valued tag %v", tag) |
| 1557 |
} |
| 1558 |
ds := f.SectionByType(SHT_DYNAMIC) |
| 1559 |
if ds == nil { |
| 1560 |
// not dynamic, so no libraries |
| 1561 |
return nil, nil |
| 1562 |
} |
| 1563 |
d, err := ds.Data() |
| 1564 |
if err != nil { |
| 1565 |
return nil, err |
| 1566 |
} |
| 1567 |
str, err := f.stringTable(ds.Link) |
| 1568 |
if err != nil { |
| 1569 |
return nil, err |
| 1570 |
} |
| 1571 |
var all []string |
| 1572 |
for len(d) > 0 { |
| 1573 |
var t DynTag |
| 1574 |
var v uint64 |
| 1575 |
switch f.Class { |
| 1576 |
case ELFCLASS32: |
| 1577 |
t = DynTag(f.ByteOrder.Uint32(d[0:4])) |
| 1578 |
v = uint64(f.ByteOrder.Uint32(d[4:8])) |
| 1579 |
d = d[8:] |
| 1580 |
case ELFCLASS64: |
| 1581 |
t = DynTag(f.ByteOrder.Uint64(d[0:8])) |
| 1582 |
v = f.ByteOrder.Uint64(d[8:16]) |
| 1583 |
d = d[16:] |
| 1584 |
} |
| 1585 |
if t == tag { |
| 1586 |
s, ok := getString(str, int(v)) |
| 1587 |
if ok { |
| 1588 |
all = append(all, s) |
| 1589 |
} |
| 1590 |
} |
| 1591 |
} |
| 1592 |
return all, nil |
| 1593 |
} |
| 1594 |
|
| 1595 |
type zeroReader struct{} |
| 1596 |
|
| 1597 |
func (*zeroReader) ReadAt(p []byte, off int64) (n int, err error) { |
| 1598 |
for i := range p { |
| 1599 |
p[i] = 0 |
| 1600 |
} |
| 1601 |
return len(p), nil |
| 1602 |
} |