Skip to content

Commit d500996

Browse files
authored
Add support for LLVM nm. (google#545)
1 parent 6609db7 commit d500996

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

internal/binutils/binutils.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,12 @@ func initTools(b *binrep, config string) {
137137
}
138138

139139
defaultPath := paths[""]
140-
b.llvmSymbolizer, b.llvmSymbolizerFound = findExe("llvm-symbolizer", append(paths["llvm-symbolizer"], defaultPath...))
141-
b.addr2line, b.addr2lineFound = findExe("addr2line", append(paths["addr2line"], defaultPath...))
142-
if !b.addr2lineFound {
143-
// On MacOS, brew installs addr2line under gaddr2line name, so search for
144-
// that if the tool is not found by its default name.
145-
b.addr2line, b.addr2lineFound = findExe("gaddr2line", append(paths["addr2line"], defaultPath...))
146-
}
147-
b.nm, b.nmFound = findExe("nm", append(paths["nm"], defaultPath...))
140+
b.llvmSymbolizer, b.llvmSymbolizerFound = chooseExe([]string{"llvm-symbolizer"}, []string{}, append(paths["llvm-symbolizer"], defaultPath...))
141+
b.addr2line, b.addr2lineFound = chooseExe([]string{"addr2line"}, []string{"gaddr2line"}, append(paths["addr2line"], defaultPath...))
142+
// The "-n" option is supported by LLVM since 2011. The output of llvm-nm
143+
// and GNU nm with "-n" option is interchangeable for our purposes, so we do
144+
// not need to differrentiate them.
145+
b.nm, b.nmFound = chooseExe([]string{"llvm-nm", "nm"}, []string{"gnm"}, append(paths["nm"], defaultPath...))
148146
b.objdump, b.objdumpFound, b.isLLVMObjdump = findObjdump(append(paths["objdump"], defaultPath...))
149147
}
150148

@@ -155,7 +153,7 @@ func initTools(b *binrep, config string) {
155153
// a string with path to the preferred objdump binary if found,
156154
// or an empty string if not found;
157155
// a boolean if any acceptable objdump was found;
158-
// a boolen indicating if it is an LLVM objdump.
156+
// a boolean indicating if it is an LLVM objdump.
159157
func findObjdump(paths []string) (string, bool, bool) {
160158
objdumpNames := []string{"llvm-objdump", "objdump"}
161159
if runtime.GOOS == "darwin" {
@@ -179,6 +177,26 @@ func findObjdump(paths []string) (string, bool, bool) {
179177
return "", false, false
180178
}
181179

180+
// chooseExe finds and returns path to preferred binary. names is a list of
181+
// names to search on both Linux and OSX. osxNames is a list of names specific
182+
// to OSX. names always has a higher priority than osxNames. The order of
183+
// the name within each list decides its priority (e.g. the first name has a
184+
// higher priority than the second name in the list).
185+
//
186+
// It returns a string with path to the binary and a boolean indicating if any
187+
// acceptable binary was found.
188+
func chooseExe(names, osxNames []string, paths []string) (string, bool) {
189+
if runtime.GOOS == "darwin" {
190+
names = append(names, osxNames...)
191+
}
192+
for _, name := range names {
193+
if binary, found := findExe(name, paths); found {
194+
return binary, true
195+
}
196+
}
197+
return "", false
198+
}
199+
182200
// isLLVMObjdump accepts a string with path to an objdump binary,
183201
// and returns a boolean indicating if the given binary is an LLVM
184202
// objdump binary of an acceptable version.

0 commit comments

Comments
 (0)