1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <libelf64/comparator.h>
18 #include <libelf64/elf64.h>
19 #include <libelf64/parse.h>
20 
21 #include <iostream>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 #include <elf.h>
27 #include <stdlib.h>
28 
usage()29 void usage() {
30     const std::string progname = getprogname();
31 
32     std::cout << "Usage: " << progname << " [shared_lib_1] [shared_lib_2]\n"
33               << R"(
34 Options:
35 shared_lib_1    elf64 shared library to compare with shared_lib_2
36 shared_lib_2    elf64 shared library to compare with shared_lib_1
37 )" << std::endl;
38 }
39 
40 // Compare ELF64 binaries (shared libraries, executables).
main(int argc,char * argv[])41 int main(int argc, char* argv[]) {
42     if (argc < 3) {
43         usage();
44         return EXIT_FAILURE;
45     }
46 
47     std::string baseSharedLibName1(argv[1]);
48     std::string baseSharedLibName2(argv[2]);
49 
50     android::elf64::Elf64Binary elf64Binary1;
51     android::elf64::Elf64Binary elf64Binary2;
52 
53     bool parse = android::elf64::Elf64Parser::ParseElfFile(baseSharedLibName1, elf64Binary1);
54     if (!parse) {
55         std::cerr << "Failed to parse file " << baseSharedLibName1 << std::endl;
56         return EXIT_FAILURE;
57     }
58 
59     parse = android::elf64::Elf64Parser::ParseElfFile(baseSharedLibName2, elf64Binary2);
60     if (!parse) {
61         std::cerr << "Failed to parse file " << baseSharedLibName2 << std::endl;
62         return EXIT_FAILURE;
63     }
64 
65     if (android::elf64::Elf64Comparator::compare(elf64Binary1.ehdr, elf64Binary2.ehdr)) {
66         std::cout << "Executable Headers are equal" << std::endl;
67     } else {
68         std::cout << "Executable Headers are NOT equal" << std::endl;
69     }
70 
71     if (android::elf64::Elf64Comparator::compare(elf64Binary1.phdrs, elf64Binary2.phdrs)) {
72         std::cout << "Program Headers are equal" << std::endl;
73     } else {
74         std::cout << "Program Headers are NOT equal" << std::endl;
75     }
76 
77     if (android::elf64::Elf64Comparator::compare(elf64Binary1.shdrs, elf64Binary2.shdrs)) {
78         std::cout << "Section Headers are equal" << std::endl;
79     } else {
80         std::cout << "Section Headers are NOT equal" << std::endl;
81     }
82 
83     if (android::elf64::Elf64Comparator::compare(elf64Binary1.sections, elf64Binary2.sections)) {
84         std::cout << "Sections are equal" << std::endl;
85     } else {
86         std::cout << "Sections are NOT equal" << std::endl;
87     }
88 
89     return 0;
90 }
91