Tests: add tests for lttng_ust_elf_is_pic
authorAntoine Busque <abusque@efficios.com>
Fri, 15 Apr 2016 17:18:04 +0000 (13:18 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 15 Apr 2016 21:43:22 +0000 (17:43 -0400)
This adds tests for the lttng_ust_elf_is_pic function used to tell
whether an executable is position independent code or not. Three
sample executables are used, one non-PIC executable, one PIE, and a
PIC shared object.

Signed-off-by: Antoine Busque <abusque@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
tests/ust-elf/README.md
tests/ust-elf/data/pic/hello.c [new file with mode: 0644]
tests/ust-elf/data/pic/hello.exec [new file with mode: 0644]
tests/ust-elf/data/pic/hello.pic [new file with mode: 0644]
tests/ust-elf/data/pic/hello.pie [new file with mode: 0644]
tests/ust-elf/data/pic/libhello.c [new file with mode: 0644]
tests/ust-elf/prog.c

index 787af8c7125848443b37e2404da06589080ab93d..0091876fe8ea3b0361f231daed8c9aff2955ea1e 100644 (file)
@@ -40,3 +40,13 @@ is added to the executable. The commands used are as follow:
     $ objcopy --only-keep-debug main.elf main.elf.debug
     $ strip -g main.elf
     $ objcopy --add-gnu-debuglink=main.elf.debug main.elf
+
+There is also a series of tests used to check detection of
+position-independent code (PIC). These tests use three pre-compiled
+ELF files found under `data/pic/`, namely `hello.exec`, `hello.pie`,
+and `hello.pic`. These can be re-generated using the files `hello.c`
+and `libhello.c`, with the following commands:
+
+    $ gcc hello.c -o hello.exec
+    $ gcc hello.c -fPIC -pie -o hello.pie
+    $ gcc -shared -o hello.pic -fPIC libhello.c
diff --git a/tests/ust-elf/data/pic/hello.c b/tests/ust-elf/data/pic/hello.c
new file mode 100644 (file)
index 0000000..e89f5fb
--- /dev/null
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+int main()
+{
+       puts("hello, world");
+
+       return 0;
+}
diff --git a/tests/ust-elf/data/pic/hello.exec b/tests/ust-elf/data/pic/hello.exec
new file mode 100644 (file)
index 0000000..9af50ea
Binary files /dev/null and b/tests/ust-elf/data/pic/hello.exec differ
diff --git a/tests/ust-elf/data/pic/hello.pic b/tests/ust-elf/data/pic/hello.pic
new file mode 100644 (file)
index 0000000..a9e3f47
Binary files /dev/null and b/tests/ust-elf/data/pic/hello.pic differ
diff --git a/tests/ust-elf/data/pic/hello.pie b/tests/ust-elf/data/pic/hello.pie
new file mode 100644 (file)
index 0000000..384795f
Binary files /dev/null and b/tests/ust-elf/data/pic/hello.pie differ
diff --git a/tests/ust-elf/data/pic/libhello.c b/tests/ust-elf/data/pic/libhello.c
new file mode 100644 (file)
index 0000000..d3f02e8
--- /dev/null
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+void hello()
+{
+       puts("hello, world");
+}
index 85b9c3474b7edcafb57ce709bff4d32aa674b5b6..305f2349ec10035987c5f30e3bf878e857f561a5 100644 (file)
@@ -26,7 +26,8 @@
 
 #define NUM_ARCH 4
 #define NUM_TESTS_PER_ARCH 11
-#define NUM_TESTS (NUM_ARCH * NUM_TESTS_PER_ARCH) + 1
+#define NUM_TESTS_PIC 3
+#define NUM_TESTS (NUM_ARCH * NUM_TESTS_PER_ARCH) + NUM_TESTS_PIC + 1
 
 /*
  * Expected memsz were computed using libelf, build ID and debug link
@@ -115,6 +116,35 @@ void test_elf(const char *test_dir, const char *arch, uint64_t exp_memsz,
        lttng_ust_elf_destroy(elf);
 }
 
+static
+void test_pic(const char *test_dir)
+{
+       char exec_path[PATH_MAX];
+       char pie_path[PATH_MAX];
+       char pic_path[PATH_MAX];
+       struct lttng_ust_elf *elf = NULL;
+       uint8_t is_pic;
+
+       snprintf(exec_path, PATH_MAX, "%s/data/pic/hello.exec", test_dir);
+       snprintf(pie_path, PATH_MAX, "%s/data/pic/hello.pie", test_dir);
+       snprintf(pic_path, PATH_MAX, "%s/data/pic/hello.pic", test_dir);
+
+       elf = lttng_ust_elf_create(exec_path);
+       is_pic = lttng_ust_elf_is_pic(elf);
+       ok(is_pic == 0, "hello.exec is not PIC");
+       lttng_ust_elf_destroy(elf);
+
+       elf = lttng_ust_elf_create(pie_path);
+       is_pic = lttng_ust_elf_is_pic(elf);
+       ok(is_pic == 1, "hello.pie is PIC");
+       lttng_ust_elf_destroy(elf);
+
+       elf = lttng_ust_elf_create(pic_path);
+       is_pic = lttng_ust_elf_is_pic(elf);
+       ok(is_pic == 1, "hello.pic is PIC");
+       lttng_ust_elf_destroy(elf);
+}
+
 int main(int argc, char **argv)
 {
        const char *test_dir;
@@ -133,6 +163,7 @@ int main(int argc, char **argv)
        test_elf(test_dir, "armeb", ARMEB_MEMSZ, armeb_build_id, ARMEB_CRC);
        test_elf(test_dir, "aarch64_be", AARCH64_BE_MEMSZ, aarch64_be_build_id,
                AARCH64_BE_CRC);
+       test_pic(test_dir);
 
        return EXIT_SUCCESS;
 }
This page took 0.027903 seconds and 4 git commands to generate.