Move to kernel style SPDX license identifiers
[lttng-ust.git] / tests / unit / ust-elf / ust-elf.c
CommitLineData
22609c7a 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-or-later
22609c7a 3 *
c0c0989a 4 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
22609c7a
AB
5 */
6
98c2069e 7#include <limits.h>
fb31eb73 8#include <stdint.h>
22609c7a
AB
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#include <lttng/ust-elf.h>
14#include "tap.h"
15
16#define NUM_ARCH 4
17#define NUM_TESTS_PER_ARCH 11
968f523d
AB
18#define NUM_TESTS_PIC 3
19#define NUM_TESTS (NUM_ARCH * NUM_TESTS_PER_ARCH) + NUM_TESTS_PIC + 1
22609c7a
AB
20
21/*
22 * Expected memsz were computed using libelf, build ID and debug link
23 * were determined through readelf.
24 */
038eca0f
AB
25#define X86_MEMSZ 5732
26#define X86_64_MEMSZ 2099376
27#define ARMEB_MEMSZ 34196
28#define AARCH64_BE_MEMSZ 67632
22609c7a
AB
29
30#define X86_CRC 0x1531f73c
31#define X86_64_CRC 0xa048a98f
32#define ARMEB_CRC 0x9d40261b
33#define AARCH64_BE_CRC 0x2b8cedce
34
35#define BUILD_ID_LEN 20
36#define DBG_FILE "main.elf.debug"
37
8d8a8c5c
MD
38static const uint8_t x86_build_id[BUILD_ID_LEN] = {
39 0x27, 0x79, 0x2a, 0xe7, 0xaa, 0xef, 0x72, 0x5c, 0x9c, 0x52,
40 0x80, 0xec, 0x1e, 0x18, 0xd8, 0x09, 0x02, 0xba, 0xbc, 0x82
41};
42static const uint8_t x86_64_build_id[BUILD_ID_LEN] = {
43 0x0f, 0x87, 0xb2, 0xe2, 0x24, 0x9c, 0xe1, 0xc2, 0x24, 0xb1,
44 0xf8, 0xb6, 0x65, 0x83, 0xa3, 0xc1, 0xcb, 0x30, 0x5c, 0x63
45};
46static const uint8_t armeb_build_id[BUILD_ID_LEN] = {
47 0x60, 0x5d, 0x26, 0xa0, 0x0e, 0x30, 0xa4, 0x29, 0xf4, 0xf1,
48 0x85, 0x53, 0xda, 0x90, 0x68, 0xe1, 0xf5, 0x67, 0xbe, 0x42
49};
50static const uint8_t aarch64_be_build_id[BUILD_ID_LEN] = {
51 0xb9, 0x0a, 0xa0, 0xed, 0xd1, 0x41, 0x42, 0xc3, 0x34, 0x85,
52 0xfa, 0x27, 0x2e, 0xa9, 0x2f, 0xd2, 0xe4, 0xf7, 0xb6, 0x60
53};
54
22609c7a
AB
55static
56void test_elf(const char *test_dir, const char *arch, uint64_t exp_memsz,
8d8a8c5c 57 const uint8_t *exp_build_id, uint32_t exp_crc)
22609c7a
AB
58{
59 char path[PATH_MAX];
60 struct lttng_ust_elf *elf = NULL;
61 int ret = 0;
62 uint64_t memsz = 0;
63 int has_build_id = 0;
64 uint8_t *build_id = NULL;
65 size_t build_id_len = 0;
66 int has_debug_link = 0;
67 char *dbg_file = NULL;
68 uint32_t crc = 0;
69
70 diag("Testing %s support", arch);
71
72 snprintf(path, PATH_MAX, "%s/data/%s/main.elf", test_dir, arch);
73 elf = lttng_ust_elf_create(path);
74 ok(elf != NULL, "lttng_ust_elf_create");
75
76 ret = lttng_ust_elf_get_memsz(elf, &memsz);
77 ok(ret == 0, "lttng_ust_elf_get_memsz returned successfully");
78 ok(memsz == exp_memsz,
79 "memsz - expected: %lu, got: %lu",
80 exp_memsz, memsz);
81
82 ret = lttng_ust_elf_get_build_id(elf, &build_id, &build_id_len,
83 &has_build_id);
84 ok(ret == 0, "lttng_ust_elf_get_build_id returned successfully");
85 ok(has_build_id == 1, "build id marked as found");
86 ok(build_id_len == BUILD_ID_LEN,
87 "build_id_len - expected: %u, got: %u",
88 BUILD_ID_LEN, build_id_len);
89 ok(memcmp(build_id, exp_build_id, build_id_len) == 0,
90 "build_id has expected value");
91
92 ret = lttng_ust_elf_get_debug_link(elf, &dbg_file, &crc,
93 &has_debug_link);
94 ok(ret == 0, "lttng_ust_elf_get_debug_link returned successfully");
95 ok(has_debug_link == 1, "debug link marked as found");
bb4f2490 96 ok(dbg_file && strcmp(dbg_file, DBG_FILE) == 0,
22609c7a
AB
97 "debug link filename - expected: %s, got: %s",
98 DBG_FILE, dbg_file);
99 ok(crc == exp_crc,
100 "debug link crc - expected: %#x, got: %#x",
101 exp_crc, crc);
102
103 free(build_id);
104 free(dbg_file);
105 lttng_ust_elf_destroy(elf);
106}
107
968f523d
AB
108static
109void test_pic(const char *test_dir)
110{
111 char exec_path[PATH_MAX];
112 char pie_path[PATH_MAX];
113 char pic_path[PATH_MAX];
114 struct lttng_ust_elf *elf = NULL;
115 uint8_t is_pic;
116
117 snprintf(exec_path, PATH_MAX, "%s/data/pic/hello.exec", test_dir);
118 snprintf(pie_path, PATH_MAX, "%s/data/pic/hello.pie", test_dir);
119 snprintf(pic_path, PATH_MAX, "%s/data/pic/hello.pic", test_dir);
120
121 elf = lttng_ust_elf_create(exec_path);
122 is_pic = lttng_ust_elf_is_pic(elf);
123 ok(is_pic == 0, "hello.exec is not PIC");
124 lttng_ust_elf_destroy(elf);
125
126 elf = lttng_ust_elf_create(pie_path);
127 is_pic = lttng_ust_elf_is_pic(elf);
128 ok(is_pic == 1, "hello.pie is PIC");
129 lttng_ust_elf_destroy(elf);
130
131 elf = lttng_ust_elf_create(pic_path);
132 is_pic = lttng_ust_elf_is_pic(elf);
133 ok(is_pic == 1, "hello.pic is PIC");
134 lttng_ust_elf_destroy(elf);
135}
136
22609c7a
AB
137int main(int argc, char **argv)
138{
62eb8bca 139 const char *test_dir;
22609c7a
AB
140
141 plan_tests(NUM_TESTS);
142
62eb8bca
MD
143 ok(argc == 2, "Invoke as: %s <path>", argv[0]);
144 if (argc != 2) {
145 return EXIT_FAILURE;
146 } else {
147 test_dir = argv[1];
148 }
149
8d8a8c5c
MD
150 test_elf(test_dir, "x86", X86_MEMSZ, x86_build_id, X86_CRC);
151 test_elf(test_dir, "x86_64", X86_64_MEMSZ, x86_64_build_id, X86_64_CRC);
152 test_elf(test_dir, "armeb", ARMEB_MEMSZ, armeb_build_id, ARMEB_CRC);
153 test_elf(test_dir, "aarch64_be", AARCH64_BE_MEMSZ, aarch64_be_build_id,
22609c7a 154 AARCH64_BE_CRC);
968f523d 155 test_pic(test_dir);
22609c7a 156
bf746e7d 157 return exit_status();
22609c7a 158}
This page took 0.031 seconds and 4 git commands to generate.