Skip to content

Commit 8b91271

Browse files
committed
Make tianon's weird ELF binaries kinda work
1 parent 42d1032 commit 8b91271

1 file changed

Lines changed: 71 additions & 69 deletions

File tree

internal/forks/elf/elf.go

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func Peek(r io.Reader) (*bufio.Reader, [16]uint8, bool, error) {
9494
}
9595

9696
func thirdPass(w io.Writer, size int64, r io.Reader, key string, f *File) error {
97+
log.Printf("thirdPass")
9798
// rw := w
9899
w = &dumbEscaper{buf: bufio.NewWriter(w)}
99100
order := ""
@@ -253,100 +254,101 @@ func thirdPass(w io.Writer, size int64, r io.Reader, key string, f *File) error
253254
}
254255

255256
func secondPass(w io.Writer, size int64, r io.Reader, key string, f *File) error {
257+
log.Printf("secondPass")
256258
if f.shstr != nil {
257259
return thirdPass(w, size, r, key, f)
258260
}
259261

260262
// Load section header string table.
261-
if f.shstrndx == 0 {
263+
if f.shstrndx != 0 {
264+
log.Printf("no section name string table")
262265
// If the file has no section name string table,
263266
// shstrndx holds the value SHN_UNDEF (0).
264-
return nil
265-
}
266-
shstr := f.Sections[f.shstrndx]
267-
if shstr.Type != SHT_STRTAB {
268-
return &FormatError{f.shoff + int64(f.shstrndx*f.shentsize), "invalid ELF section name string table type", shstr.Type}
269-
}
267+
shstr := f.Sections[f.shstrndx]
268+
if shstr.Type != SHT_STRTAB {
269+
return &FormatError{f.shoff + int64(f.shstrndx*f.shentsize), "invalid ELF section name string table type", shstr.Type}
270+
}
270271

271-
sr := &seekForward{
272-
r: r,
273-
size: size,
274-
cap: 24,
275-
}
272+
sr := &seekForward{
273+
r: r,
274+
size: size,
275+
cap: 24,
276+
}
276277

277-
sections := []*Section{shstr}
278+
sections := []*Section{shstr}
278279

279-
for _, typ := range []SectionType{SHT_DYNAMIC, SHT_DYNSYM, SHT_SYMTAB, SHT_GNU_VERDEF, SHT_GNU_VERNEED, SHT_GNU_VERSYM} {
280-
s := f.SectionByType(typ)
281-
if s == nil {
282-
continue
283-
}
280+
for _, typ := range []SectionType{SHT_DYNAMIC, SHT_DYNSYM, SHT_SYMTAB, SHT_GNU_VERDEF, SHT_GNU_VERNEED, SHT_GNU_VERSYM} {
281+
s := f.SectionByType(typ)
282+
if s == nil {
283+
continue
284+
}
284285

285-
sections = append(sections, s)
286-
}
286+
sections = append(sections, s)
287+
}
287288

288-
for _, s := range sections {
289-
if s.Link != 0 {
290-
sections = append(sections, f.Sections[s.Link])
289+
for _, s := range sections {
290+
if s.Link != 0 {
291+
sections = append(sections, f.Sections[s.Link])
292+
}
291293
}
292-
}
293294

294-
slices.SortFunc(sections, func(a, b *Section) int {
295-
return cmp.Compare(a.Offset, b.Offset)
296-
})
295+
slices.SortFunc(sections, func(a, b *Section) int {
296+
return cmp.Compare(a.Offset, b.Offset)
297+
})
297298

298-
for _, s := range sections {
299-
if err := s.init(sr); err != nil {
300-
return fmt.Errorf("section.init(): %w", err)
299+
for _, s := range sections {
300+
if err := s.init(sr); err != nil {
301+
return fmt.Errorf("section.init(): %w", err)
302+
}
301303
}
302-
}
303-
304-
// We need these sections.
305-
// SHT_DYNAMIC
306-
// SHT_SYMTAB
307-
// SHT_STRTAB
308-
//
309-
// We need the string tables from their Link fields.
310-
// And we need to access those all in order.
311304

312-
log.Printf("DynamicSection")
313-
ds, err := f.DynamicSection(sr)
314-
if err != nil {
315-
return err
316-
}
317-
f.dynamicSection = ds
305+
// We need these sections.
306+
// SHT_DYNAMIC
307+
// SHT_SYMTAB
308+
// SHT_STRTAB
309+
//
310+
// We need the string tables from their Link fields.
311+
// And we need to access those all in order.
318312

319-
log.Printf("DynamicSymbols")
320-
if _, err := f.DynamicSymbols(sr); err != nil {
321-
if !errors.Is(err, ErrNoSymbols) {
313+
log.Printf("DynamicSection")
314+
ds, err := f.DynamicSection(sr)
315+
if err != nil {
322316
return err
323317
}
324-
}
318+
f.dynamicSection = ds
325319

326-
log.Printf("Symbols")
327-
symbols, err := f.Symbols(sr)
328-
if err != nil {
329-
if !errors.Is(err, ErrNoSymbols) {
330-
return err
320+
log.Printf("DynamicSymbols")
321+
if _, err := f.DynamicSymbols(sr); err != nil {
322+
if !errors.Is(err, ErrNoSymbols) {
323+
return err
324+
}
331325
}
332-
}
333-
f.symbols = symbols
334326

335-
log.Printf("shstr.Data()")
336-
shstrtab, err := shstr.Data(sr)
337-
if err != nil {
338-
return fmt.Errorf("shstr.Data(): %w", err)
339-
}
340-
f.shstr = shstrtab
341-
for i, s := range f.Sections {
342-
var ok bool
343-
s.Name, ok = getString(shstrtab, int(f.names[i]))
344-
if !ok {
345-
return &FormatError{f.shoff + int64(i*f.shentsize), "bad section name index", f.names[i]}
327+
log.Printf("Symbols")
328+
symbols, err := f.Symbols(sr)
329+
if err != nil {
330+
if !errors.Is(err, ErrNoSymbols) {
331+
return err
332+
}
346333
}
347-
}
334+
f.symbols = symbols
348335

349-
cached.Store(key, f)
336+
log.Printf("shstr.Data()")
337+
shstrtab, err := shstr.Data(sr)
338+
if err != nil {
339+
return fmt.Errorf("shstr.Data(): %w", err)
340+
}
341+
f.shstr = shstrtab
342+
for i, s := range f.Sections {
343+
var ok bool
344+
s.Name, ok = getString(shstrtab, int(f.names[i]))
345+
if !ok {
346+
return &FormatError{f.shoff + int64(i*f.shentsize), "bad section name index", f.names[i]}
347+
}
348+
}
349+
350+
cached.Store(key, f)
351+
}
350352

351353
return thirdPass(w, size, r, key, f)
352354
}

0 commit comments

Comments
 (0)