Skip to content

Commit b0c278b

Browse files
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>
1 parent 0ff3231 commit b0c278b

File tree

1 file changed

+12
-9
lines changed
  • src/cmd/link/internal/ld

1 file changed

+12
-9
lines changed

‎src/cmd/link/internal/ld/elf.go‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,16 @@ func elf32shdr(out *OutBuf, e *ElfShdr) {
365365

366366
func elfwriteshdrs(out *OutBuf) uint32 {
367367
if elf64 {
368-
for i := 0; i < int(ehdr.Shnum); i++ {
369-
elf64shdr(out, shdr[i])
368+
for _, sh := range shdr {
369+
elf64shdr(out, sh)
370370
}
371-
return uint32(ehdr.Shnum) * ELF64SHDRSIZE
371+
return uint32(len(shdr)) * ELF64SHDRSIZE
372372
}
373373

374-
for i := 0; i < int(ehdr.Shnum); i++ {
375-
elf32shdr(out, shdr[i])
374+
for _, sh := range shdr {
375+
elf32shdr(out, sh)
376376
}
377-
return uint32(ehdr.Shnum) * ELF32SHDRSIZE
377+
return uint32(len(shdr)) * ELF32SHDRSIZE
378378
}
379379

380380
// elfSortShdrs sorts the section headers so that allocated sections
@@ -460,7 +460,6 @@ func newElfShdr(name int64) *ElfShdr {
460460
e.Name = uint32(name)
461461
e.shnum = -1 // make invalid for now, set by elfSortShdrs
462462
shdr = append(shdr, e)
463-
ehdr.Shnum++
464463
return e
465464
}
466465

@@ -1172,8 +1171,7 @@ func elfshname(name string) *ElfShdr {
11721171
continue
11731172
}
11741173
off := elfstr[i].off
1175-
for i = 0; i < int(ehdr.Shnum); i++ {
1176-
sh := shdr[i]
1174+
for _, sh := range shdr {
11771175
if sh.Name == uint32(off) {
11781176
return sh
11791177
}
@@ -2380,6 +2378,11 @@ elfobj:
23802378
pph.Memsz = pph.Filesz
23812379
}
23822380

2381+
if len(shdr) >= 0xffff {
2382+
Errorf("too many ELF sections")
2383+
}
2384+
eh.Shnum = uint16(len(shdr))
2385+
23832386
ctxt.Out.SeekSet(0)
23842387
a := int64(0)
23852388
a += int64(elfwritehdr(ctxt.Out))

0 commit comments

Comments
 (0)