github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/src/debug/elf/file.go (about) 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 "debug/dwarf" 11 "encoding/binary" 12 "errors" 13 "fmt" 14 "io" 15 "os" 16 ) 17 18 // TODO: error reporting detail 19 20 /* 21 * Internal ELF representation 22 */ 23 24 // A FileHeader represents an ELF file header. 25 type FileHeader struct { 26 Class Class 27 Data Data 28 Version Version 29 OSABI OSABI 30 ABIVersion uint8 31 ByteOrder binary.ByteOrder 32 Type Type 33 Machine Machine 34 Entry uint64 35 } 36 37 // A File represents an open ELF file. 38 type File struct { 39 FileHeader 40 Sections []*Section 41 Progs []*Prog 42 closer io.Closer 43 gnuNeed []verneed 44 gnuVersym []byte 45 } 46 47 // A SectionHeader represents a single ELF section header. 48 type SectionHeader struct { 49 Name string 50 Type SectionType 51 Flags SectionFlag 52 Addr uint64 53 Offset uint64 54 Size uint64 55 Link uint32 56 Info uint32 57 Addralign uint64 58 Entsize uint64 59 } 60 61 // A Section represents a single section in an ELF file. 62 type Section struct { 63 SectionHeader 64 65 // Embed ReaderAt for ReadAt method. 66 // Do not embed SectionReader directly 67 // to avoid having Read and Seek. 68 // If a client wants Read and Seek it must use 69 // Open() to avoid fighting over the seek offset 70 // with other clients. 71 io.ReaderAt 72 sr *io.SectionReader 73 } 74 75 // Data reads and returns the contents of the ELF section. 76 func (s *Section) Data() ([]byte, error) { 77 dat := make([]byte, s.sr.Size()) 78 n, err := s.sr.ReadAt(dat, 0) 79 if n == len(dat) { 80 err = nil 81 } 82 return dat[0:n], err 83 } 84 85 // stringTable reads and returns the string table given by the 86 // specified link value. 87 func (f *File) stringTable(link uint32) ([]byte, error) { 88 if link <= 0 || link >= uint32(len(f.Sections)) { 89 return nil, errors.New("section has invalid string table link") 90 } 91 return f.Sections[link].Data() 92 } 93 94 // Open returns a new ReadSeeker reading the ELF section. 95 func (s *Section) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) } 96 97 // A ProgHeader represents a single ELF program header. 98 type ProgHeader struct { 99 Type ProgType 100 Flags ProgFlag 101 Off uint64 102 Vaddr uint64 103 Paddr uint64 104 Filesz uint64 105 Memsz uint64 106 Align uint64 107 } 108 109 // A Prog represents a single ELF program header in an ELF binary. 110 type Prog struct { 111 ProgHeader 112 113 // Embed ReaderAt for ReadAt method. 114 // Do not embed SectionReader directly 115 // to avoid having Read and Seek. 116 // If a client wants Read and Seek it must use 117 // Open() to avoid fighting over the seek offset 118 // with other clients. 119 io.ReaderAt 120 sr *io.SectionReader 121 } 122 123 // Open returns a new ReadSeeker reading the ELF program body. 124 func (p *Prog) Open() io.ReadSeeker { return io.NewSectionReader(p.sr, 0, 1<<63-1) } 125 126 // A Symbol represents an entry in an ELF symbol table section. 127 type Symbol struct { 128 Name string 129 Info, Other byte 130 Section SectionIndex 131 Value, Size uint64 132 } 133 134 /* 135 * ELF reader 136 */ 137 138 type FormatError struct { 139 off int64 140 msg string 141 val interface{} 142 } 143 144 func (e *FormatError) Error() string { 145 msg := e.msg 146 if e.val != nil { 147 msg += fmt.Sprintf(" '%v' ", e.val) 148 } 149 msg += fmt.Sprintf("in record at byte %#x", e.off) 150 return msg 151 } 152 153 // Open opens the named file using os.Open and prepares it for use as an ELF binary. 154 func Open(name string) (*File, error) { 155 f, err := os.Open(name) 156 if err != nil { 157 return nil, err 158 } 159 ff, err := NewFile(f) 160 if err != nil { 161 f.Close() 162 return nil, err 163 } 164 ff.closer = f 165 return ff, nil 166 } 167 168 // Close closes the File. 169 // If the File was created using NewFile directly instead of Open, 170 // Close has no effect. 171 func (f *File) Close() error { 172 var err error 173 if f.closer != nil { 174 err = f.closer.Close() 175 f.closer = nil 176 } 177 return err 178 } 179 180 // SectionByType returns the first section in f with the 181 // given type, or nil if there is no such section. 182 func (f *File) SectionByType(typ SectionType) *Section { 183 for _, s := range f.Sections { 184 if s.Type == typ { 185 return s 186 } 187 } 188 return nil 189 } 190 191 // NewFile creates a new File for accessing an ELF binary in an underlying reader. 192 // The ELF binary is expected to start at position 0 in the ReaderAt. 193 func NewFile(r io.ReaderAt) (*File, error) { 194 sr := io.NewSectionReader(r, 0, 1<<63-1) 195 // Read and decode ELF identifier 196 var ident [16]uint8 197 if _, err := r.ReadAt(ident[0:], 0); err != nil { 198 return nil, err 199 } 200 if ident[0] != '\x7f' || ident[1] != 'E' || ident[2] != 'L' || ident[3] != 'F' { 201 return nil, &FormatError{0, "bad magic number", ident[0:4]} 202 } 203 204 f := new(File) 205 f.Class = Class(ident[EI_CLASS]) 206 switch f.Class { 207 case ELFCLASS32: 208 case ELFCLASS64: 209 // ok 210 default: 211 return nil, &FormatError{0, "unknown ELF class", f.Class} 212 } 213 214 f.Data = Data(ident[EI_DATA]) 215 switch f.Data { 216 case ELFDATA2LSB: 217 f.ByteOrder = binary.LittleEndian 218 case ELFDATA2MSB: 219 f.ByteOrder = binary.BigEndian 220 default: 221 return nil, &FormatError{0, "unknown ELF data encoding", f.Data} 222 } 223 224 f.Version = Version(ident[EI_VERSION]) 225 if f.Version != EV_CURRENT { 226 return nil, &FormatError{0, "unknown ELF version", f.Version} 227 } 228 229 f.OSABI = OSABI(ident[EI_OSABI]) 230 f.ABIVersion = ident[EI_ABIVERSION] 231 232 // Read ELF file header 233 var phoff int64 234 var phentsize, phnum int 235 var shoff int64 236 var shentsize, shnum, shstrndx int 237 shstrndx = -1 238 switch f.Class { 239 case ELFCLASS32: 240 hdr := new(Header32) 241 sr.Seek(0, os.SEEK_SET) 242 if err := binary.Read(sr, f.ByteOrder, hdr); err != nil { 243 return nil, err 244 } 245 f.Type = Type(hdr.Type) 246 f.Machine = Machine(hdr.Machine) 247 f.Entry = uint64(hdr.Entry) 248 if v := Version(hdr.Version); v != f.Version { 249 return nil, &FormatError{0, "mismatched ELF version", v} 250 } 251 phoff = int64(hdr.Phoff) 252 phentsize = int(hdr.Phentsize) 253 phnum = int(hdr.Phnum) 254 shoff = int64(hdr.Shoff) 255 shentsize = int(hdr.Shentsize) 256 shnum = int(hdr.Shnum) 257 shstrndx = int(hdr.Shstrndx) 258 case ELFCLASS64: 259 hdr := new(Header64) 260 sr.Seek(0, os.SEEK_SET) 261 if err := binary.Read(sr, f.ByteOrder, hdr); err != nil { 262 return nil, err 263 } 264 f.Type = Type(hdr.Type) 265 f.Machine = Machine(hdr.Machine) 266 f.Entry = uint64(hdr.Entry) 267 if v := Version(hdr.Version); v != f.Version { 268 return nil, &FormatError{0, "mismatched ELF version", v} 269 } 270 phoff = int64(hdr.Phoff) 271 phentsize = int(hdr.Phentsize) 272 phnum = int(hdr.Phnum) 273 shoff = int64(hdr.Shoff) 274 shentsize = int(hdr.Shentsize) 275 shnum = int(hdr.Shnum) 276 shstrndx = int(hdr.Shstrndx) 277 } 278 279 if shnum > 0 && shoff > 0 && (shstrndx < 0 || shstrndx >= shnum) { 280 return nil, &FormatError{0, "invalid ELF shstrndx", shstrndx} 281 } 282 283 // Read program headers 284 f.Progs = make([]*Prog, phnum) 285 for i := 0; i < phnum; i++ { 286 off := phoff + int64(i)*int64(phentsize) 287 sr.Seek(off, os.SEEK_SET) 288 p := new(Prog) 289 switch f.Class { 290 case ELFCLASS32: 291 ph := new(Prog32) 292 if err := binary.Read(sr, f.ByteOrder, ph); err != nil { 293 return nil, err 294 } 295 p.ProgHeader = ProgHeader{ 296 Type: ProgType(ph.Type), 297 Flags: ProgFlag(ph.Flags), 298 Off: uint64(ph.Off), 299 Vaddr: uint64(ph.Vaddr), 300 Paddr: uint64(ph.Paddr), 301 Filesz: uint64(ph.Filesz), 302 Memsz: uint64(ph.Memsz), 303 Align: uint64(ph.Align), 304 } 305 case ELFCLASS64: 306 ph := new(Prog64) 307 if err := binary.Read(sr, f.ByteOrder, ph); err != nil { 308 return nil, err 309 } 310 p.ProgHeader = ProgHeader{ 311 Type: ProgType(ph.Type), 312 Flags: ProgFlag(ph.Flags), 313 Off: uint64(ph.Off), 314 Vaddr: uint64(ph.Vaddr), 315 Paddr: uint64(ph.Paddr), 316 Filesz: uint64(ph.Filesz), 317 Memsz: uint64(ph.Memsz), 318 Align: uint64(ph.Align), 319 } 320 } 321 p.sr = io.NewSectionReader(r, int64(p.Off), int64(p.Filesz)) 322 p.ReaderAt = p.sr 323 f.Progs[i] = p 324 } 325 326 // Read section headers 327 f.Sections = make([]*Section, shnum) 328 names := make([]uint32, shnum) 329 for i := 0; i < shnum; i++ { 330 off := shoff + int64(i)*int64(shentsize) 331 sr.Seek(off, os.SEEK_SET) 332 s := new(Section) 333 switch f.Class { 334 case ELFCLASS32: 335 sh := new(Section32) 336 if err := binary.Read(sr, f.ByteOrder, sh); err != nil { 337 return nil, err 338 } 339 names[i] = sh.Name 340 s.SectionHeader = SectionHeader{ 341 Type: SectionType(sh.Type), 342 Flags: SectionFlag(sh.Flags), 343 Addr: uint64(sh.Addr), 344 Offset: uint64(sh.Off), 345 Size: uint64(sh.Size), 346 Link: uint32(sh.Link), 347 Info: uint32(sh.Info), 348 Addralign: uint64(sh.Addralign), 349 Entsize: uint64(sh.Entsize), 350 } 351 case ELFCLASS64: 352 sh := new(Section64) 353 if err := binary.Read(sr, f.ByteOrder, sh); err != nil { 354 return nil, err 355 } 356 names[i] = sh.Name 357 s.SectionHeader = SectionHeader{ 358 Type: SectionType(sh.Type), 359 Flags: SectionFlag(sh.Flags), 360 Offset: uint64(sh.Off), 361 Size: uint64(sh.Size), 362 Addr: uint64(sh.Addr), 363 Link: uint32(sh.Link), 364 Info: uint32(sh.Info), 365 Addralign: uint64(sh.Addralign), 366 Entsize: uint64(sh.Entsize), 367 } 368 } 369 s.sr = io.NewSectionReader(r, int64(s.Offset), int64(s.Size)) 370 s.ReaderAt = s.sr 371 f.Sections[i] = s 372 } 373 374 if len(f.Sections) == 0 { 375 return f, nil 376 } 377 378 // Load section header string table. 379 shstrtab, err := f.Sections[shstrndx].Data() 380 if err != nil { 381 return nil, err 382 } 383 for i, s := range f.Sections { 384 var ok bool 385 s.Name, ok = getString(shstrtab, int(names[i])) 386 if !ok { 387 return nil, &FormatError{shoff + int64(i*shentsize), "bad section name index", names[i]} 388 } 389 } 390 391 return f, nil 392 } 393 394 // getSymbols returns a slice of Symbols from parsing the symbol table 395 // with the given type, along with the associated string table. 396 func (f *File) getSymbols(typ SectionType) ([]Symbol, []byte, error) { 397 switch f.Class { 398 case ELFCLASS64: 399 return f.getSymbols64(typ) 400 401 case ELFCLASS32: 402 return f.getSymbols32(typ) 403 } 404 405 return nil, nil, errors.New("not implemented") 406 } 407 408 // ErrNoSymbols is returned by File.Symbols and File.DynamicSymbols 409 // if there is no such section in the File. 410 var ErrNoSymbols = errors.New("no symbol section") 411 412 func (f *File) getSymbols32(typ SectionType) ([]Symbol, []byte, error) { 413 symtabSection := f.SectionByType(typ) 414 if symtabSection == nil { 415 return nil, nil, ErrNoSymbols 416 } 417 418 data, err := symtabSection.Data() 419 if err != nil { 420 return nil, nil, errors.New("cannot load symbol section") 421 } 422 symtab := bytes.NewReader(data) 423 if symtab.Len()%Sym32Size != 0 { 424 return nil, nil, errors.New("length of symbol section is not a multiple of SymSize") 425 } 426 427 strdata, err := f.stringTable(symtabSection.Link) 428 if err != nil { 429 return nil, nil, errors.New("cannot load string table section") 430 } 431 432 // The first entry is all zeros. 433 var skip [Sym32Size]byte 434 symtab.Read(skip[:]) 435 436 symbols := make([]Symbol, symtab.Len()/Sym32Size) 437 438 i := 0 439 var sym Sym32 440 for symtab.Len() > 0 { 441 binary.Read(symtab, f.ByteOrder, &sym) 442 str, _ := getString(strdata, int(sym.Name)) 443 symbols[i].Name = str 444 symbols[i].Info = sym.Info 445 symbols[i].Other = sym.Other 446 symbols[i].Section = SectionIndex(sym.Shndx) 447 symbols[i].Value = uint64(sym.Value) 448 symbols[i].Size = uint64(sym.Size) 449 i++ 450 } 451 452 return symbols, strdata, nil 453 } 454 455 func (f *File) getSymbols64(typ SectionType) ([]Symbol, []byte, error) { 456 symtabSection := f.SectionByType(typ) 457 if symtabSection == nil { 458 return nil, nil, ErrNoSymbols 459 } 460 461 data, err := symtabSection.Data() 462 if err != nil { 463 return nil, nil, errors.New("cannot load symbol section") 464 } 465 symtab := bytes.NewReader(data) 466 if symtab.Len()%Sym64Size != 0 { 467 return nil, nil, errors.New("length of symbol section is not a multiple of Sym64Size") 468 } 469 470 strdata, err := f.stringTable(symtabSection.Link) 471 if err != nil { 472 return nil, nil, errors.New("cannot load string table section") 473 } 474 475 // The first entry is all zeros. 476 var skip [Sym64Size]byte 477 symtab.Read(skip[:]) 478 479 symbols := make([]Symbol, symtab.Len()/Sym64Size) 480 481 i := 0 482 var sym Sym64 483 for symtab.Len() > 0 { 484 binary.Read(symtab, f.ByteOrder, &sym) 485 str, _ := getString(strdata, int(sym.Name)) 486 symbols[i].Name = str 487 symbols[i].Info = sym.Info 488 symbols[i].Other = sym.Other 489 symbols[i].Section = SectionIndex(sym.Shndx) 490 symbols[i].Value = sym.Value 491 symbols[i].Size = sym.Size 492 i++ 493 } 494 495 return symbols, strdata, nil 496 } 497 498 // getString extracts a string from an ELF string table. 499 func getString(section []byte, start int) (string, bool) { 500 if start < 0 || start >= len(section) { 501 return "", false 502 } 503 504 for end := start; end < len(section); end++ { 505 if section[end] == 0 { 506 return string(section[start:end]), true 507 } 508 } 509 return "", false 510 } 511 512 // Section returns a section with the given name, or nil if no such 513 // section exists. 514 func (f *File) Section(name string) *Section { 515 for _, s := range f.Sections { 516 if s.Name == name { 517 return s 518 } 519 } 520 return nil 521 } 522 523 // applyRelocations applies relocations to dst. rels is a relocations section 524 // in RELA format. 525 func (f *File) applyRelocations(dst []byte, rels []byte) error { 526 if f.Class == ELFCLASS64 && f.Machine == EM_X86_64 { 527 return f.applyRelocationsAMD64(dst, rels) 528 } 529 if f.Class == ELFCLASS32 && f.Machine == EM_386 { 530 return f.applyRelocations386(dst, rels) 531 } 532 if f.Class == ELFCLASS64 && f.Machine == EM_AARCH64 { 533 return f.applyRelocationsARM64(dst, rels) 534 } 535 536 return errors.New("not implemented") 537 } 538 539 func (f *File) applyRelocationsAMD64(dst []byte, rels []byte) error { 540 // 24 is the size of Rela64. 541 if len(rels)%24 != 0 { 542 return errors.New("length of relocation section is not a multiple of 24") 543 } 544 545 symbols, _, err := f.getSymbols(SHT_SYMTAB) 546 if err != nil { 547 return err 548 } 549 550 b := bytes.NewReader(rels) 551 var rela Rela64 552 553 for b.Len() > 0 { 554 binary.Read(b, f.ByteOrder, &rela) 555 symNo := rela.Info >> 32 556 t := R_X86_64(rela.Info & 0xffff) 557 558 if symNo == 0 || symNo > uint64(len(symbols)) { 559 continue 560 } 561 sym := &symbols[symNo-1] 562 if SymType(sym.Info&0xf) != STT_SECTION { 563 // We don't handle non-section relocations for now. 564 continue 565 } 566 567 // There are relocations, so this must be a normal 568 // object file, and we only look at section symbols, 569 // so we assume that the symbol value is 0. 570 571 switch t { 572 case R_X86_64_64: 573 if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { 574 continue 575 } 576 f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], uint64(rela.Addend)) 577 case R_X86_64_32: 578 if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { 579 continue 580 } 581 f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], uint32(rela.Addend)) 582 } 583 } 584 585 return nil 586 } 587 588 func (f *File) applyRelocations386(dst []byte, rels []byte) error { 589 // 8 is the size of Rel32. 590 if len(rels)%8 != 0 { 591 return errors.New("length of relocation section is not a multiple of 8") 592 } 593 594 symbols, _, err := f.getSymbols(SHT_SYMTAB) 595 if err != nil { 596 return err 597 } 598 599 b := bytes.NewReader(rels) 600 var rel Rel32 601 602 for b.Len() > 0 { 603 binary.Read(b, f.ByteOrder, &rel) 604 symNo := rel.Info >> 8 605 t := R_386(rel.Info & 0xff) 606 607 if symNo == 0 || symNo > uint32(len(symbols)) { 608 continue 609 } 610 sym := &symbols[symNo-1] 611 612 if t == R_386_32 { 613 if rel.Off+4 >= uint32(len(dst)) { 614 continue 615 } 616 val := f.ByteOrder.Uint32(dst[rel.Off : rel.Off+4]) 617 val += uint32(sym.Value) 618 f.ByteOrder.PutUint32(dst[rel.Off:rel.Off+4], val) 619 } 620 } 621 622 return nil 623 } 624 625 func (f *File) applyRelocationsARM64(dst []byte, rels []byte) error { 626 // 24 is the size of Rela64. 627 if len(rels)%24 != 0 { 628 return errors.New("length of relocation section is not a multiple of 24") 629 } 630 631 symbols, _, err := f.getSymbols(SHT_SYMTAB) 632 if err != nil { 633 return err 634 } 635 636 b := bytes.NewReader(rels) 637 var rela Rela64 638 639 for b.Len() > 0 { 640 binary.Read(b, f.ByteOrder, &rela) 641 symNo := rela.Info >> 32 642 t := R_AARCH64(rela.Info & 0xffff) 643 644 if symNo == 0 || symNo > uint64(len(symbols)) { 645 continue 646 } 647 sym := &symbols[symNo-1] 648 if SymType(sym.Info&0xf) != STT_SECTION { 649 // We don't handle non-section relocations for now. 650 continue 651 } 652 653 // There are relocations, so this must be a normal 654 // object file, and we only look at section symbols, 655 // so we assume that the symbol value is 0. 656 657 switch t { 658 case R_AARCH64_ABS64: 659 if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 { 660 continue 661 } 662 f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], uint64(rela.Addend)) 663 case R_AARCH64_ABS32: 664 if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 { 665 continue 666 } 667 f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], uint32(rela.Addend)) 668 } 669 } 670 671 return nil 672 } 673 674 func (f *File) DWARF() (*dwarf.Data, error) { 675 // There are many other DWARF sections, but these 676 // are the required ones, and the debug/dwarf package 677 // does not use the others, so don't bother loading them. 678 var names = [...]string{"abbrev", "info", "str"} 679 var dat [len(names)][]byte 680 for i, name := range names { 681 name = ".debug_" + name 682 s := f.Section(name) 683 if s == nil { 684 continue 685 } 686 b, err := s.Data() 687 if err != nil && uint64(len(b)) < s.Size { 688 return nil, err 689 } 690 dat[i] = b 691 } 692 693 // If there's a relocation table for .debug_info, we have to process it 694 // now otherwise the data in .debug_info is invalid for x86-64 objects. 695 rela := f.Section(".rela.debug_info") 696 if rela != nil && rela.Type == SHT_RELA && (f.Machine == EM_X86_64 || f.Machine == EM_AARCH64) { 697 data, err := rela.Data() 698 if err != nil { 699 return nil, err 700 } 701 err = f.applyRelocations(dat[1], data) 702 if err != nil { 703 return nil, err 704 } 705 } 706 707 // When using clang we need to process relocations even for 386. 708 rel := f.Section(".rel.debug_info") 709 if rel != nil && rel.Type == SHT_REL && f.Machine == EM_386 { 710 data, err := rel.Data() 711 if err != nil { 712 return nil, err 713 } 714 err = f.applyRelocations(dat[1], data) 715 if err != nil { 716 return nil, err 717 } 718 } 719 720 abbrev, info, str := dat[0], dat[1], dat[2] 721 d, err := dwarf.New(abbrev, nil, nil, info, nil, nil, nil, str) 722 if err != nil { 723 return nil, err 724 } 725 726 // Look for DWARF4 .debug_types sections. 727 for i, s := range f.Sections { 728 if s.Name == ".debug_types" { 729 b, err := s.Data() 730 if err != nil && uint64(len(b)) < s.Size { 731 return nil, err 732 } 733 734 for _, r := range f.Sections { 735 if r.Type != SHT_RELA && r.Type != SHT_REL { 736 continue 737 } 738 if int(r.Info) != i { 739 continue 740 } 741 rd, err := r.Data() 742 if err != nil { 743 return nil, err 744 } 745 err = f.applyRelocations(b, rd) 746 if err != nil { 747 return nil, err 748 } 749 } 750 751 err = d.AddTypes(fmt.Sprintf("types-%d", i), b) 752 if err != nil { 753 return nil, err 754 } 755 } 756 } 757 758 return d, nil 759 } 760 761 // Symbols returns the symbol table for f. The symbols will be listed in the order 762 // they appear in f. 763 // 764 // For compatibility with Go 1.0, Symbols omits the null symbol at index 0. 765 // After retrieving the symbols as symtab, an externally supplied index x 766 // corresponds to symtab[x-1], not symtab[x]. 767 func (f *File) Symbols() ([]Symbol, error) { 768 sym, _, err := f.getSymbols(SHT_SYMTAB) 769 return sym, err 770 } 771 772 // DynamicSymbols returns the dynamic symbol table for f. The symbols 773 // will be listed in the order they appear in f. 774 // 775 // For compatibility with Symbols, DynamicSymbols omits the null symbol at index 0. 776 // After retrieving the symbols as symtab, an externally supplied index x 777 // corresponds to symtab[x-1], not symtab[x]. 778 func (f *File) DynamicSymbols() ([]Symbol, error) { 779 sym, _, err := f.getSymbols(SHT_DYNSYM) 780 return sym, err 781 } 782 783 type ImportedSymbol struct { 784 Name string 785 Version string 786 Library string 787 } 788 789 // ImportedSymbols returns the names of all symbols 790 // referred to by the binary f that are expected to be 791 // satisfied by other libraries at dynamic load time. 792 // It does not return weak symbols. 793 func (f *File) ImportedSymbols() ([]ImportedSymbol, error) { 794 sym, str, err := f.getSymbols(SHT_DYNSYM) 795 if err != nil { 796 return nil, err 797 } 798 f.gnuVersionInit(str) 799 var all []ImportedSymbol 800 for i, s := range sym { 801 if ST_BIND(s.Info) == STB_GLOBAL && s.Section == SHN_UNDEF { 802 all = append(all, ImportedSymbol{Name: s.Name}) 803 f.gnuVersion(i, &all[len(all)-1]) 804 } 805 } 806 return all, nil 807 } 808 809 type verneed struct { 810 File string 811 Name string 812 } 813 814 // gnuVersionInit parses the GNU version tables 815 // for use by calls to gnuVersion. 816 func (f *File) gnuVersionInit(str []byte) { 817 // Accumulate verneed information. 818 vn := f.SectionByType(SHT_GNU_VERNEED) 819 if vn == nil { 820 return 821 } 822 d, _ := vn.Data() 823 824 var need []verneed 825 i := 0 826 for { 827 if i+16 > len(d) { 828 break 829 } 830 vers := f.ByteOrder.Uint16(d[i : i+2]) 831 if vers != 1 { 832 break 833 } 834 cnt := f.ByteOrder.Uint16(d[i+2 : i+4]) 835 fileoff := f.ByteOrder.Uint32(d[i+4 : i+8]) 836 aux := f.ByteOrder.Uint32(d[i+8 : i+12]) 837 next := f.ByteOrder.Uint32(d[i+12 : i+16]) 838 file, _ := getString(str, int(fileoff)) 839 840 var name string 841 j := i + int(aux) 842 for c := 0; c < int(cnt); c++ { 843 if j+16 > len(d) { 844 break 845 } 846 // hash := f.ByteOrder.Uint32(d[j:j+4]) 847 // flags := f.ByteOrder.Uint16(d[j+4:j+6]) 848 other := f.ByteOrder.Uint16(d[j+6 : j+8]) 849 nameoff := f.ByteOrder.Uint32(d[j+8 : j+12]) 850 next := f.ByteOrder.Uint32(d[j+12 : j+16]) 851 name, _ = getString(str, int(nameoff)) 852 ndx := int(other) 853 if ndx >= len(need) { 854 a := make([]verneed, 2*(ndx+1)) 855 copy(a, need) 856 need = a 857 } 858 859 need[ndx] = verneed{file, name} 860 if next == 0 { 861 break 862 } 863 j += int(next) 864 } 865 866 if next == 0 { 867 break 868 } 869 i += int(next) 870 } 871 872 // Versym parallels symbol table, indexing into verneed. 873 vs := f.SectionByType(SHT_GNU_VERSYM) 874 if vs == nil { 875 return 876 } 877 d, _ = vs.Data() 878 879 f.gnuNeed = need 880 f.gnuVersym = d 881 } 882 883 // gnuVersion adds Library and Version information to sym, 884 // which came from offset i of the symbol table. 885 func (f *File) gnuVersion(i int, sym *ImportedSymbol) { 886 // Each entry is two bytes. 887 i = (i + 1) * 2 888 if i >= len(f.gnuVersym) { 889 return 890 } 891 j := int(f.ByteOrder.Uint16(f.gnuVersym[i:])) 892 if j < 2 || j >= len(f.gnuNeed) { 893 return 894 } 895 n := &f.gnuNeed[j] 896 sym.Library = n.File 897 sym.Version = n.Name 898 } 899 900 // ImportedLibraries returns the names of all libraries 901 // referred to by the binary f that are expected to be 902 // linked with the binary at dynamic link time. 903 func (f *File) ImportedLibraries() ([]string, error) { 904 return f.DynString(DT_NEEDED) 905 } 906 907 // DynString returns the strings listed for the given tag in the file's dynamic 908 // section. 909 // 910 // The tag must be one that takes string values: DT_NEEDED, DT_SONAME, DT_RPATH, or 911 // DT_RUNPATH. 912 func (f *File) DynString(tag DynTag) ([]string, error) { 913 switch tag { 914 case DT_NEEDED, DT_SONAME, DT_RPATH, DT_RUNPATH: 915 default: 916 return nil, fmt.Errorf("non-string-valued tag %v", tag) 917 } 918 ds := f.SectionByType(SHT_DYNAMIC) 919 if ds == nil { 920 // not dynamic, so no libraries 921 return nil, nil 922 } 923 d, err := ds.Data() 924 if err != nil { 925 return nil, err 926 } 927 str, err := f.stringTable(ds.Link) 928 if err != nil { 929 return nil, err 930 } 931 var all []string 932 for len(d) > 0 { 933 var t DynTag 934 var v uint64 935 switch f.Class { 936 case ELFCLASS32: 937 t = DynTag(f.ByteOrder.Uint32(d[0:4])) 938 v = uint64(f.ByteOrder.Uint32(d[4:8])) 939 d = d[8:] 940 case ELFCLASS64: 941 t = DynTag(f.ByteOrder.Uint64(d[0:8])) 942 v = f.ByteOrder.Uint64(d[8:16]) 943 d = d[16:] 944 } 945 if t == tag { 946 s, ok := getString(str, int(v)) 947 if ok { 948 all = append(all, s) 949 } 950 } 951 } 952 return all, nil 953 }