1// Copyright 2017 The BoringSSL Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
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