xref: /aosp_15_r20/external/boringssl/src/util/check_imported_libraries.go (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1// Copyright (c) 2017, Google Inc.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15//go:build ignore
16
17// check_imported_libraries.go checks that each of its arguments only imports
18// allowed libraries. This is used to avoid accidental dependencies on
19// libstdc++.so.
20package main
21
22import (
23	"debug/elf"
24	"fmt"
25	"os"
26	"path/filepath"
27)
28
29func checkImportedLibraries(path string) bool {
30	file, err := elf.Open(path)
31	if err != nil {
32		fmt.Fprintf(os.Stderr, "Error opening %s: %s\n", path, err)
33		return false
34	}
35	defer file.Close()
36
37	libs, err := file.ImportedLibraries()
38	if err != nil {
39		fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", path, err)
40		return false
41	}
42
43	allowCpp := filepath.Base(path) == "libssl.so"
44	for _, lib := range libs {
45		if lib == "libc.so.6" || lib == "libcrypto.so" || lib == "libpthread.so.0" || lib == "libgcc_s.so.1" {
46			continue
47		}
48		if allowCpp && lib == "libstdc++.so.6" {
49			continue
50		}
51		fmt.Printf("Invalid dependency for %s: %s\n", path, lib)
52		fmt.Printf("All dependencies:\n")
53		for _, lib := range libs {
54			fmt.Printf("    %s\n", lib)
55		}
56		return false
57	}
58	return true
59}
60
61func main() {
62	ok := true
63	for _, path := range os.Args[1:] {
64		if !checkImportedLibraries(path) {
65			ok = false
66		}
67	}
68	if !ok {
69		os.Exit(1)
70	}
71}
72