• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
27
28func checkImportedLibraries(path string) {
29	file, err := elf.Open(path)
30	if err != nil {
31		fmt.Fprintf(os.Stderr, "Error opening %s: %s\n", path, err)
32		os.Exit(1)
33	}
34	defer file.Close()
35
36	libs, err := file.ImportedLibraries()
37	if err != nil {
38		fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", path, err)
39		os.Exit(1)
40	}
41
42	for _, lib := range libs {
43		if lib != "libc.so.6" && lib != "libcrypto.so" && lib != "libpthread.so.0" {
44			fmt.Printf("Invalid dependency for %s: %s\n", path, lib)
45			fmt.Printf("All dependencies:\n")
46			for _, lib := range libs {
47				fmt.Printf("    %s\n", lib)
48			}
49			os.Exit(1)
50		}
51	}
52}
53
54func main() {
55	for _, path := range os.Args[1:] {
56		checkImportedLibraries(path)
57	}
58}
59