4036563ae44b9fa6dd8a7f32a8c1b63b41b1e23d
[lttng-ust.git] / tests / ust-elf / ust-elf.c
1 /*
2 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <lttng/ust-elf.h>
25 #include "tap.h"
26
27 #define NUM_ARCH 4
28 #define NUM_TESTS_PER_ARCH 11
29 #define NUM_TESTS_PIC 3
30 #define NUM_TESTS (NUM_ARCH * NUM_TESTS_PER_ARCH) + NUM_TESTS_PIC + 1
31
32 /*
33 * Expected memsz were computed using libelf, build ID and debug link
34 * were determined through readelf.
35 */
36 #define X86_MEMSZ 5732
37 #define X86_64_MEMSZ 2099376
38 #define ARMEB_MEMSZ 34196
39 #define AARCH64_BE_MEMSZ 67632
40
41 #define X86_CRC 0x1531f73c
42 #define X86_64_CRC 0xa048a98f
43 #define ARMEB_CRC 0x9d40261b
44 #define AARCH64_BE_CRC 0x2b8cedce
45
46 #define BUILD_ID_LEN 20
47 #define DBG_FILE "main.elf.debug"
48
49 static const uint8_t x86_build_id[BUILD_ID_LEN] = {
50 0x27, 0x79, 0x2a, 0xe7, 0xaa, 0xef, 0x72, 0x5c, 0x9c, 0x52,
51 0x80, 0xec, 0x1e, 0x18, 0xd8, 0x09, 0x02, 0xba, 0xbc, 0x82
52 };
53 static const uint8_t x86_64_build_id[BUILD_ID_LEN] = {
54 0x0f, 0x87, 0xb2, 0xe2, 0x24, 0x9c, 0xe1, 0xc2, 0x24, 0xb1,
55 0xf8, 0xb6, 0x65, 0x83, 0xa3, 0xc1, 0xcb, 0x30, 0x5c, 0x63
56 };
57 static const uint8_t armeb_build_id[BUILD_ID_LEN] = {
58 0x60, 0x5d, 0x26, 0xa0, 0x0e, 0x30, 0xa4, 0x29, 0xf4, 0xf1,
59 0x85, 0x53, 0xda, 0x90, 0x68, 0xe1, 0xf5, 0x67, 0xbe, 0x42
60 };
61 static const uint8_t aarch64_be_build_id[BUILD_ID_LEN] = {
62 0xb9, 0x0a, 0xa0, 0xed, 0xd1, 0x41, 0x42, 0xc3, 0x34, 0x85,
63 0xfa, 0x27, 0x2e, 0xa9, 0x2f, 0xd2, 0xe4, 0xf7, 0xb6, 0x60
64 };
65
66 static
67 void test_elf(const char *test_dir, const char *arch, uint64_t exp_memsz,
68 const uint8_t *exp_build_id, uint32_t exp_crc)
69 {
70 char path[PATH_MAX];
71 struct lttng_ust_elf *elf = NULL;
72 int ret = 0;
73 uint64_t memsz = 0;
74 int has_build_id = 0;
75 uint8_t *build_id = NULL;
76 size_t build_id_len = 0;
77 int has_debug_link = 0;
78 char *dbg_file = NULL;
79 uint32_t crc = 0;
80
81 diag("Testing %s support", arch);
82
83 snprintf(path, PATH_MAX, "%s/data/%s/main.elf", test_dir, arch);
84 elf = lttng_ust_elf_create(path);
85 ok(elf != NULL, "lttng_ust_elf_create");
86
87 ret = lttng_ust_elf_get_memsz(elf, &memsz);
88 ok(ret == 0, "lttng_ust_elf_get_memsz returned successfully");
89 ok(memsz == exp_memsz,
90 "memsz - expected: %lu, got: %lu",
91 exp_memsz, memsz);
92
93 ret = lttng_ust_elf_get_build_id(elf, &build_id, &build_id_len,
94 &has_build_id);
95 ok(ret == 0, "lttng_ust_elf_get_build_id returned successfully");
96 ok(has_build_id == 1, "build id marked as found");
97 ok(build_id_len == BUILD_ID_LEN,
98 "build_id_len - expected: %u, got: %u",
99 BUILD_ID_LEN, build_id_len);
100 ok(memcmp(build_id, exp_build_id, build_id_len) == 0,
101 "build_id has expected value");
102
103 ret = lttng_ust_elf_get_debug_link(elf, &dbg_file, &crc,
104 &has_debug_link);
105 ok(ret == 0, "lttng_ust_elf_get_debug_link returned successfully");
106 ok(has_debug_link == 1, "debug link marked as found");
107 ok(dbg_file && strcmp(dbg_file, DBG_FILE) == 0,
108 "debug link filename - expected: %s, got: %s",
109 DBG_FILE, dbg_file);
110 ok(crc == exp_crc,
111 "debug link crc - expected: %#x, got: %#x",
112 exp_crc, crc);
113
114 free(build_id);
115 free(dbg_file);
116 lttng_ust_elf_destroy(elf);
117 }
118
119 static
120 void test_pic(const char *test_dir)
121 {
122 char exec_path[PATH_MAX];
123 char pie_path[PATH_MAX];
124 char pic_path[PATH_MAX];
125 struct lttng_ust_elf *elf = NULL;
126 uint8_t is_pic;
127
128 snprintf(exec_path, PATH_MAX, "%s/data/pic/hello.exec", test_dir);
129 snprintf(pie_path, PATH_MAX, "%s/data/pic/hello.pie", test_dir);
130 snprintf(pic_path, PATH_MAX, "%s/data/pic/hello.pic", test_dir);
131
132 elf = lttng_ust_elf_create(exec_path);
133 is_pic = lttng_ust_elf_is_pic(elf);
134 ok(is_pic == 0, "hello.exec is not PIC");
135 lttng_ust_elf_destroy(elf);
136
137 elf = lttng_ust_elf_create(pie_path);
138 is_pic = lttng_ust_elf_is_pic(elf);
139 ok(is_pic == 1, "hello.pie is PIC");
140 lttng_ust_elf_destroy(elf);
141
142 elf = lttng_ust_elf_create(pic_path);
143 is_pic = lttng_ust_elf_is_pic(elf);
144 ok(is_pic == 1, "hello.pic is PIC");
145 lttng_ust_elf_destroy(elf);
146 }
147
148 int main(int argc, char **argv)
149 {
150 const char *test_dir;
151
152 plan_tests(NUM_TESTS);
153
154 ok(argc == 2, "Invoke as: %s <path>", argv[0]);
155 if (argc != 2) {
156 return EXIT_FAILURE;
157 } else {
158 test_dir = argv[1];
159 }
160
161 test_elf(test_dir, "x86", X86_MEMSZ, x86_build_id, X86_CRC);
162 test_elf(test_dir, "x86_64", X86_64_MEMSZ, x86_64_build_id, X86_64_CRC);
163 test_elf(test_dir, "armeb", ARMEB_MEMSZ, armeb_build_id, ARMEB_CRC);
164 test_elf(test_dir, "aarch64_be", AARCH64_BE_MEMSZ, aarch64_be_build_id,
165 AARCH64_BE_CRC);
166 test_pic(test_dir);
167
168 return EXIT_SUCCESS;
169 }
This page took 0.033162 seconds and 3 git commands to generate.