-
Notifications
You must be signed in to change notification settings - Fork 5
/
sold_main.cc
111 lines (99 loc) · 3.82 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
// 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-fini Do not use .fini_array of the ELF file
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},
{0, 0, 0, 0},
};
std::string input_file;
std::string output_file;
std::vector<std::string> exclude_sos;
std::vector<std::string> exclude_finis;
std::vector<std::string> custome_library_path;
bool emit_section_header = false;
bool check_output = 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 '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_finis, custome_library_path, emit_section_header);
sold.Link(output_file);
if (check_output) {
std::string dummy = output_file + ".dummy-for-check-output";
Sold check(output_file, exclude_sos, exclude_finis, custome_library_path, emit_section_header);
check.Link(dummy);
std::remove(dummy.c_str());
}
return 0;
}