@@ -225,7 +225,11 @@ func NewFile(r io.ReaderAt) (*File, error) {
225
225
if _ , err := sr .Seek (int64 (hdrsz )+ int64 (opthdr ), io .SeekStart ); err != nil {
226
226
return nil , err
227
227
}
228
- f .Sections = make ([]* Section , nscns )
228
+ c := saferio .SliceCap ((* * Section )(nil ), uint64 (nscns ))
229
+ if c < 0 {
230
+ return nil , fmt .Errorf ("too many XCOFF sections (%d)" , nscns )
231
+ }
232
+ f .Sections = make ([]* Section , 0 , c )
229
233
for i := 0 ; i < int (nscns ); i ++ {
230
234
var scnptr uint64
231
235
s := new (Section )
@@ -261,7 +265,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
261
265
}
262
266
s .sr = io .NewSectionReader (r2 , int64 (scnptr ), int64 (s .Size ))
263
267
s .ReaderAt = s .sr
264
- f .Sections [ i ] = s
268
+ f .Sections = append ( f . Sections , s )
265
269
}
266
270
267
271
// Symbol map needed by relocation
@@ -388,52 +392,59 @@ func NewFile(r io.ReaderAt) (*File, error) {
388
392
389
393
// Read relocations
390
394
// Only for .data or .text section
391
- for _ , sect := range f .Sections {
395
+ for sectNum , sect := range f .Sections {
392
396
if sect .Type != STYP_TEXT && sect .Type != STYP_DATA {
393
397
continue
394
398
}
395
- sect .Relocs = make ([]Reloc , sect .Nreloc )
396
399
if sect .Relptr == 0 {
397
400
continue
398
401
}
402
+ c := saferio .SliceCap ((* Reloc )(nil ), uint64 (sect .Nreloc ))
403
+ if c < 0 {
404
+ return nil , fmt .Errorf ("too many relocs (%d) for section %d" , sect .Nreloc , sectNum )
405
+ }
406
+ sect .Relocs = make ([]Reloc , 0 , c )
399
407
if _ , err := sr .Seek (int64 (sect .Relptr ), io .SeekStart ); err != nil {
400
408
return nil , err
401
409
}
402
410
for i := uint32 (0 ); i < sect .Nreloc ; i ++ {
411
+ var reloc Reloc
403
412
switch f .TargetMachine {
404
413
case U802TOCMAGIC :
405
414
rel := new (Reloc32 )
406
415
if err := binary .Read (sr , binary .BigEndian , rel ); err != nil {
407
416
return nil , err
408
417
}
409
- sect . Relocs [ i ] .VirtualAddress = uint64 (rel .Rvaddr )
410
- sect . Relocs [ i ] .Symbol = idxToSym [int (rel .Rsymndx )]
411
- sect . Relocs [ i ] .Type = rel .Rtype
412
- sect . Relocs [ i ] .Length = rel .Rsize & 0x3F + 1
418
+ reloc .VirtualAddress = uint64 (rel .Rvaddr )
419
+ reloc .Symbol = idxToSym [int (rel .Rsymndx )]
420
+ reloc .Type = rel .Rtype
421
+ reloc .Length = rel .Rsize & 0x3F + 1
413
422
414
423
if rel .Rsize & 0x80 != 0 {
415
- sect . Relocs [ i ] .Signed = true
424
+ reloc .Signed = true
416
425
}
417
426
if rel .Rsize & 0x40 != 0 {
418
- sect . Relocs [ i ] .InstructionFixed = true
427
+ reloc .InstructionFixed = true
419
428
}
420
429
421
430
case U64_TOCMAGIC :
422
431
rel := new (Reloc64 )
423
432
if err := binary .Read (sr , binary .BigEndian , rel ); err != nil {
424
433
return nil , err
425
434
}
426
- sect . Relocs [ i ] .VirtualAddress = rel .Rvaddr
427
- sect . Relocs [ i ] .Symbol = idxToSym [int (rel .Rsymndx )]
428
- sect . Relocs [ i ] .Type = rel .Rtype
429
- sect . Relocs [ i ] .Length = rel .Rsize & 0x3F + 1
435
+ reloc .VirtualAddress = rel .Rvaddr
436
+ reloc .Symbol = idxToSym [int (rel .Rsymndx )]
437
+ reloc .Type = rel .Rtype
438
+ reloc .Length = rel .Rsize & 0x3F + 1
430
439
if rel .Rsize & 0x80 != 0 {
431
- sect . Relocs [ i ] .Signed = true
440
+ reloc .Signed = true
432
441
}
433
442
if rel .Rsize & 0x40 != 0 {
434
- sect . Relocs [ i ] .InstructionFixed = true
443
+ reloc .InstructionFixed = true
435
444
}
436
445
}
446
+
447
+ sect .Relocs = append (sect .Relocs , reloc )
437
448
}
438
449
}
439
450
0 commit comments