diff options
| author | Ian Lance Taylor <iant@golang.org> | 2025-11-07 11:20:15 -0800 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2025-11-26 20:00:38 -0800 |
| commit | b0c278be4072b2aec941e4600852f7a5ff40fe22 (patch) | |
| tree | 6def3fc59c2c4c16d187884151d7ab72cbb6a22b /src/cmd/link/internal/ld | |
| parent | 0ff323143de9d6915a8abec441009cecd803e442 (diff) | |
| download | go-b0c278be4072b2aec941e4600852f7a5ff40fe22.tar.xz | |
cmd/link: use shdr as a slice rather than counting in elfhdr.Shnum
Change-Id: I293e50e3a6ab19fb927099e106095d6aa1241b9f
Reviewed-on: https://go-review.googlesource.com/c/go/+/718820
Reviewed-by: Junyang Shao <shaojunyang@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/cmd/link/internal/ld')
| -rw-r--r-- | src/cmd/link/internal/ld/elf.go | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/cmd/link/internal/ld/elf.go b/src/cmd/link/internal/ld/elf.go index ba0c181daf..6bda9f0ef5 100644 --- a/src/cmd/link/internal/ld/elf.go +++ b/src/cmd/link/internal/ld/elf.go @@ -365,16 +365,16 @@ func elf32shdr(out *OutBuf, e *ElfShdr) { func elfwriteshdrs(out *OutBuf) uint32 { if elf64 { - for i := 0; i < int(ehdr.Shnum); i++ { - elf64shdr(out, shdr[i]) + for _, sh := range shdr { + elf64shdr(out, sh) } - return uint32(ehdr.Shnum) * ELF64SHDRSIZE + return uint32(len(shdr)) * ELF64SHDRSIZE } - for i := 0; i < int(ehdr.Shnum); i++ { - elf32shdr(out, shdr[i]) + for _, sh := range shdr { + elf32shdr(out, sh) } - return uint32(ehdr.Shnum) * ELF32SHDRSIZE + return uint32(len(shdr)) * ELF32SHDRSIZE } // elfSortShdrs sorts the section headers so that allocated sections @@ -460,7 +460,6 @@ func newElfShdr(name int64) *ElfShdr { e.Name = uint32(name) e.shnum = -1 // make invalid for now, set by elfSortShdrs shdr = append(shdr, e) - ehdr.Shnum++ return e } @@ -1172,8 +1171,7 @@ func elfshname(name string) *ElfShdr { continue } off := elfstr[i].off - for i = 0; i < int(ehdr.Shnum); i++ { - sh := shdr[i] + for _, sh := range shdr { if sh.Name == uint32(off) { return sh } @@ -2380,6 +2378,11 @@ elfobj: pph.Memsz = pph.Filesz } + if len(shdr) >= 0xffff { + Errorf("too many ELF sections") + } + eh.Shnum = uint16(len(shdr)) + ctxt.Out.SeekSet(0) a := int64(0) a += int64(elfwritehdr(ctxt.Out)) |
