Native Image SBOM: support extracting symbols in .dynsym section for ELF files (#3647)

Signed-off-by: Joel Rudsberg <joel.rudsberg@oracle.com>
This commit is contained in:
Joel Rudsberg 2025-03-06 15:12:14 +01:00 committed by GitHub
parent 36c198ac67
commit 974ce23722
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -251,11 +251,11 @@ func (ni nativeImageElf) fetchPkgs() (pkgs []pkg.Package, relationships []artifa
var sbomLength elf.Symbol var sbomLength elf.Symbol
var svmVersion elf.Symbol var svmVersion elf.Symbol
si, err := bi.Symbols() si, err := ni.getSymbols()
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("no symbols found in binary: %w", err) return nil, nil, err
} }
if si == nil { if len(si) == 0 {
return nil, nil, errors.New(nativeImageMissingSymbolsError) return nil, nil, errors.New(nativeImageMissingSymbolsError)
} }
for _, s := range si { for _, s := range si {
@ -286,6 +286,31 @@ func (ni nativeImageElf) fetchPkgs() (pkgs []pkg.Package, relationships []artifa
return decompressSbom(data, sbomLocation, lengthLocation) return decompressSbom(data, sbomLocation, lengthLocation)
} }
// getSymbols obtains the union of the symbols in the .symtab and .dynsym sections of the ELF file
func (ni nativeImageElf) getSymbols() ([]elf.Symbol, error) {
var symbols []elf.Symbol
symsErr := error(nil)
dynErr := error(nil)
if syms, err := ni.file.Symbols(); err == nil {
symbols = append(symbols, syms...)
} else {
symsErr = err
}
if dynSyms, err := ni.file.DynamicSymbols(); err == nil {
symbols = append(symbols, dynSyms...)
} else {
dynErr = err
}
if symsErr != nil && dynErr != nil {
return nil, fmt.Errorf("could not retrieve symbols from binary: SHT_SYMTAB error: %v, SHT_DYNSYM error: %v", symsErr, dynErr)
}
return symbols, nil
}
// fetchPkgs obtains the packages from a Native Image given as a Mach O file. // fetchPkgs obtains the packages from a Native Image given as a Mach O file.
func (ni nativeImageMachO) fetchPkgs() (pkgs []pkg.Package, relationships []artifact.Relationship, retErr error) { func (ni nativeImageMachO) fetchPkgs() (pkgs []pkg.Package, relationships []artifact.Relationship, retErr error) {
defer func() { defer func() {