-
Notifications
You must be signed in to change notification settings - Fork 0
/
sold_main.cc
139 lines (127 loc) · 5.21 KB
/
sold_main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (C) 2021 The sold authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "sold.h"
#include <getopt.h>
void print_help(std::ostream& os) {
os << R"(usage: sold [option] [input]
Options:
-h, --help Show this help message and exit
-o, --output-file OUTPUT_FILE Specify the ELF file to output (this option is mandatory)
-i, --input-file INPUT_FILE Specify the ELF file to input
-e, --exclude-so EXCLUDE_FILE Specify the ELF file to exclude (e.g. libmax.so)
-L, --custom-library-path PATH Use PATH instead of the default path such as /usr/lib
--section-headers Emit section headers
--check-output Check the output using sold itself
--exclude-from-init Do not use .init_array of the shared object.
This option is only for debug.
--exclude-from-fini Do not use .fini_array of the shared object.
This option is only for debug.
--exclude-runpath-contains Exclude paths from DT_RUNPATH contains the argument
--delete-unused-PT_DYNAMIC Zero pads unused PT_DYNAMIC
--exclude-dir Exclude all SOs in this directory
The last argument is interpreted as SOURCE_FILE when -i option isn't given.
)" << std::endl;
}
int main(int argc, char* const argv[]) {
google::InitGoogleLogging(argv[0]);
static option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"input-file", required_argument, nullptr, 'i'},
{"output-file", required_argument, nullptr, 'o'},
{"exclude-so", required_argument, nullptr, 'e'},
{"custom-library-path", required_argument, nullptr, 'L'},
{"section-headers", no_argument, nullptr, 1},
{"check-output", no_argument, nullptr, 2},
{"exclude-from-fini", required_argument, nullptr, 3},
{"exclude-runpath-contains", required_argument, nullptr, 4},
{"delete-unused-DT_STRTAB", no_argument, nullptr, 5},
{"exclude-dir", required_argument, nullptr, 6},
{"exclude-from-init", required_argument, nullptr, 7},
{0, 0, 0, 0},
};
std::string input_file;
std::string output_file;
std::vector<std::string> exclude_sos;
std::vector<std::string> exclude_dirs;
std::vector<std::string> exclude_inits;
std::vector<std::string> exclude_finis;
std::vector<std::string> custome_library_path;
std::vector<std::string> exclude_runpath_pattern;
bool emit_section_header = false;
bool check_output = false;
bool delete_unused_DT_STRTAB = false;
int opt;
while ((opt = getopt_long(argc, argv, "hi:o:e:", long_options, nullptr)) != -1) {
switch (opt) {
case 1:
emit_section_header = true;
break;
case 2:
check_output = true;
break;
case 3:
exclude_finis.push_back(optarg);
break;
case 4:
exclude_runpath_pattern.emplace_back(optarg);
break;
case 5:
delete_unused_DT_STRTAB = true;
break;
case 6:
exclude_dirs.emplace_back(optarg);
break;
case 7:
exclude_inits.push_back(optarg);
break;
case 'e':
exclude_sos.push_back(optarg);
break;
case 'L':
custome_library_path.emplace_back(optarg);
break;
case 'i':
input_file = optarg;
break;
case 'o':
output_file = optarg;
break;
case 'h':
print_help(std::cout);
return 0;
case '?':
print_help(std::cerr);
return 1;
}
}
if (optind < argc && input_file.empty()) {
input_file = argv[optind++];
}
if (output_file == "") {
std::cerr << "You must specify the output file." << std::endl;
return 1;
}
Sold sold(input_file, exclude_sos, exclude_dirs, exclude_inits, exclude_finis, custome_library_path, exclude_runpath_pattern,
emit_section_header, delete_unused_DT_STRTAB);
sold.Link(output_file);
if (check_output) {
std::string dummy = output_file + ".dummy-for-check-output";
Sold check(output_file, exclude_sos, exclude_dirs, exclude_inits, exclude_finis, custome_library_path, exclude_runpath_pattern,
emit_section_header, delete_unused_DT_STRTAB);
check.Link(dummy);
std::remove(dummy.c_str());
}
return 0;
}