From 941ff1dbc780dd994428d4d7d9bb669eb77af409 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 14 Mar 2024 21:17:21 -0700 Subject: [PATCH] Move ELF build-id reader into a separate library. Bug: 328702178 Change-Id: I188a8d20d22e67e4f0c7e3441e3781fff369c828 --- cmd/symbols_map/Android.bp | 3 +-- cmd/symbols_map/symbols_map.go | 3 ++- elf/Android.bp | 28 ++++++++++++++++++++++++++++ {cmd/symbols_map => elf}/elf.go | 6 +++--- {cmd/symbols_map => elf}/elf_test.go | 2 +- 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 elf/Android.bp rename {cmd/symbols_map => elf}/elf.go (94%) rename {cmd/symbols_map => elf}/elf_test.go (99%) diff --git a/cmd/symbols_map/Android.bp b/cmd/symbols_map/Android.bp index 0ba3b07a8..e3ae6ede5 100644 --- a/cmd/symbols_map/Android.bp +++ b/cmd/symbols_map/Android.bp @@ -5,17 +5,16 @@ package { blueprint_go_binary { name: "symbols_map", srcs: [ - "elf.go", "r8.go", "symbols_map.go", ], testSrcs: [ - "elf_test.go", "r8_test.go", ], deps: [ "blueprint-pathtools", "golang-protobuf-encoding-prototext", + "soong-elf", "soong-response", "symbols_map_proto", ], diff --git a/cmd/symbols_map/symbols_map.go b/cmd/symbols_map/symbols_map.go index 938446d48..c56cf93e8 100644 --- a/cmd/symbols_map/symbols_map.go +++ b/cmd/symbols_map/symbols_map.go @@ -22,6 +22,7 @@ import ( "strings" "android/soong/cmd/symbols_map/symbols_map_proto" + "android/soong/elf" "android/soong/response" "github.com/google/blueprint/pathtools" @@ -116,7 +117,7 @@ func main() { if *elfFile != "" { typ = symbols_map_proto.Mapping_ELF location = *elfFile - identifier, err = elfIdentifier(*elfFile, true) + identifier, err = elf.Identifier(*elfFile, true) if err != nil { fmt.Fprintf(os.Stderr, "error reading elf identifier: %s\n", err) os.Exit(1) diff --git a/elf/Android.bp b/elf/Android.bp new file mode 100644 index 000000000..6450be137 --- /dev/null +++ b/elf/Android.bp @@ -0,0 +1,28 @@ +// Copyright 2016 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +bootstrap_go_package { + name: "soong-elf", + pkgPath: "android/soong/elf", + srcs: [ + "elf.go", + ], + testSrcs: [ + "elf_test.go", + ], +} diff --git a/cmd/symbols_map/elf.go b/elf/elf.go similarity index 94% rename from cmd/symbols_map/elf.go rename to elf/elf.go index 950e3b246..e84a8aeea 100644 --- a/cmd/symbols_map/elf.go +++ b/elf/elf.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package elf import ( "debug/elf" @@ -26,9 +26,9 @@ import ( const gnuBuildID = "GNU\x00" -// elfIdentifier extracts the elf build ID from an elf file. If allowMissing is true it returns +// Identifier extracts the elf build ID from an elf file. If allowMissing is true it returns // an empty identifier if the file exists but the build ID note does not. -func elfIdentifier(filename string, allowMissing bool) (string, error) { +func Identifier(filename string, allowMissing bool) (string, error) { f, err := os.Open(filename) if err != nil { return "", fmt.Errorf("failed to open %s: %w", filename, err) diff --git a/cmd/symbols_map/elf_test.go b/elf/elf_test.go similarity index 99% rename from cmd/symbols_map/elf_test.go rename to elf/elf_test.go index a94c87f21..a22077090 100644 --- a/cmd/symbols_map/elf_test.go +++ b/elf/elf_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package elf import ( "bytes"