Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is pic #23

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions doc/man/lttng-ust.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -777,18 +777,20 @@ This event has no fields.
This event has no fields.

`lttng_ust_statedump:soinfo`::
Emitted when information about a currently loaded shared object is
found.
Emitted when information about a currently loaded executable or
shared object is found.
+
Fields:
+
[options="header"]
|==============================================================
|==================================================================
| Field name | Description
| `baddr` | Base address of loaded library
| `memsz` | Size of loaded library in memory
| `sopath` | Path to loaded library file
|==============================================================
| `baddr` | Base address of loaded executable
| `memsz` | Size of loaded executable in memory
| `sopath` | Path to loaded executable file
| `is_pic` | Whether the executable is
position-independent code
|==================================================================

`lttng_ust_statedump:build_id`::
Emitted when a build ID is found in a currently loaded shared
Expand Down
1 change: 1 addition & 0 deletions include/lttng/ust-elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ int is_elf_native_endian(struct lttng_ust_elf *elf)

struct lttng_ust_elf *lttng_ust_elf_create(const char *path);
void lttng_ust_elf_destroy(struct lttng_ust_elf *elf);
uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf *elf);
int lttng_ust_elf_get_memsz(struct lttng_ust_elf *elf, uint64_t *memsz);
int lttng_ust_elf_get_build_id(struct lttng_ust_elf *elf, uint8_t **build_id,
size_t *length, int *found);
Expand Down
10 changes: 10 additions & 0 deletions liblttng-ust/lttng-ust-elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@ struct lttng_ust_elf *lttng_ust_elf_create(const char *path)
return NULL;
}

/*
* Test whether the ELF file is position independent code (PIC)
*/
uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf *elf)
{
/* PIC has and e_type value of ET_DYN, see ELF specification
* version 1.1 p. 1-3. */
return elf->ehdr->e_type == ET_DYN;
}

/*
* Destroy the given lttng_ust_elf instance.
*/
Expand Down
4 changes: 3 additions & 1 deletion liblttng-ust/lttng-ust-statedump-provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ TRACEPOINT_EVENT(lttng_ust_statedump, soinfo,
struct lttng_session *, session,
void *, baddr,
const char*, sopath,
uint64_t, memsz
uint64_t, memsz,
uint8_t, is_pic
),
TP_FIELDS(
ctf_integer_hex(void *, baddr, baddr)
ctf_integer(uint64_t, memsz, memsz)
ctf_string(sopath, sopath)
ctf_integer(uint8_t, is_pic, is_pic)
)
)

Expand Down
5 changes: 4 additions & 1 deletion liblttng-ust/lttng-ust-statedump.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct soinfo_data {
size_t build_id_len;
int vdso;
uint32_t crc;
uint8_t is_pic;
};

typedef void (*tracepoint_cb)(struct lttng_session *session, void *priv);
Expand Down Expand Up @@ -84,7 +85,7 @@ void trace_soinfo_cb(struct lttng_session *session, void *priv)

tracepoint(lttng_ust_statedump, soinfo,
session, so_data->base_addr_ptr,
so_data->resolved_path, so_data->memsz);
so_data->resolved_path, so_data->memsz, so_data->is_pic);
}

static
Expand Down Expand Up @@ -147,6 +148,8 @@ int get_elf_info(struct soinfo_data *so_data, int *has_build_id,
goto end;
}

so_data->is_pic = lttng_ust_elf_is_pic(elf);

end:
lttng_ust_elf_destroy(elf);
return ret;
Expand Down
16 changes: 15 additions & 1 deletion tests/ust-elf/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ prog_LDADD = $(top_builddir)/liblttng-ust/liblttng-ust.la \

noinst_SCRIPTS = test_ust_elf
CLEANFILES = $(noinst_SCRIPTS)
EXTRA_DIST = test_ust_elf.in $(srcdir)/data/**
EXTRA_DIST = test_ust_elf.in \
$(srcdir)/data/main.c \
$(srcdir)/data/aarch64_be/main.elf \
$(srcdir)/data/aarch64_be/main.elf.debug \
$(srcdir)/data/armeb/main.elf \
$(srcdir)/data/armeb/main.elf.debug \
$(srcdir)/data/x86/main.elf \
$(srcdir)/data/x86/main.elf.debug \
$(srcdir)/data/x86_64/main.elf \
$(srcdir)/data/x86_64/main.elf.debug \
$(srcdir)/data/pic/hello.exec \
$(srcdir)/data/pic/hello.pie \
$(srcdir)/data/pic/hello.pic \
$(srcdir)/data/pic/hello.c \
$(srcdir)/data/pic/libhello.c

$(noinst_SCRIPTS): %: %.in
sed "s#@ABSTOPSRCDIR@#$(abs_top_srcdir)#g" < $< > $@
Expand Down
10 changes: 10 additions & 0 deletions tests/ust-elf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions tests/ust-elf/data/pic/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>

int main()
{
puts("hello, world");

return 0;
}
Binary file added tests/ust-elf/data/pic/hello.exec
Binary file not shown.
Binary file added tests/ust-elf/data/pic/hello.pic
Binary file not shown.
Binary file added tests/ust-elf/data/pic/hello.pie
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/ust-elf/data/pic/libhello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

void hello()
{
puts("hello, world");
}
33 changes: 32 additions & 1 deletion tests/ust-elf/prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}