| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************** | ||
| 2 | Auteur : Pierre Aubert | ||
| 3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
| 4 | Licence : CeCILL-C | ||
| 5 | ****************************************/ | ||
| 6 | |||
| 7 | #include <stdio.h> | ||
| 8 | |||
| 9 | // DL includes | ||
| 10 | #include <link.h> | ||
| 11 | #include <stdlib.h> | ||
| 12 | #include <dlfcn.h> | ||
| 13 | #include <elf.h> | ||
| 14 | |||
| 15 | #include <iostream> | ||
| 16 | |||
| 17 | #include "phoenix_dl.h" | ||
| 18 | |||
| 19 | ///List the symbols in the given library | ||
| 20 | /** @param[out] vecFunction : vector of function names in the given library | ||
| 21 | * @param libFileName : name of the library to be loaded | ||
| 22 | */ | ||
| 23 | 3 | void phoenix_listLibrarySymbol(std::vector<std::string> & vecFunction, const std::string & libFileName){ | |
| 24 |
3/3✓ Branch 0 (2→3) taken 3 times.
✓ Branch 2 (3→4) taken 1 times.
✓ Branch 3 (3→5) taken 2 times.
|
4 | if(libFileName == ""){return;} |
| 25 | 2 | void* library = dlopen(libFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL); | |
| 26 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→13) taken 1 times.
|
2 | if(library == NULL){ |
| 27 |
4/4✓ Branch 0 (8→9) taken 1 times.
✓ Branch 2 (9→10) taken 1 times.
✓ Branch 4 (10→11) taken 1 times.
✓ Branch 6 (11→12) taken 1 times.
|
1 | std::cerr << "phoenix_listLibrarySymbol : library '"<<libFileName<<"' not found" << std::endl; |
| 28 | 1 | return; | |
| 29 | } | ||
| 30 | 1 | struct link_map * map = NULL; | |
| 31 | 1 | dlinfo(library, RTLD_DI_LINKMAP, &map); | |
| 32 | |||
| 33 | 1 | Elf64_Sym * symtab = nullptr; | |
| 34 | 1 | char * strtab = nullptr; | |
| 35 | 1 | int symentries = 0; | |
| 36 |
2/2✓ Branch 0 (22→15) taken 29 times.
✓ Branch 1 (22→23) taken 1 times.
|
30 | for(auto section = map->l_ld; section->d_tag != DT_NULL; ++section){ |
| 37 |
2/2✓ Branch 0 (15→16) taken 1 times.
✓ Branch 1 (15→17) taken 28 times.
|
29 | if(section->d_tag == DT_SYMTAB){ |
| 38 | 1 | symtab = (Elf64_Sym *)section->d_un.d_ptr; | |
| 39 | } | ||
| 40 |
2/2✓ Branch 0 (17→18) taken 1 times.
✓ Branch 1 (17→19) taken 28 times.
|
29 | if(section->d_tag == DT_STRTAB){ |
| 41 | 1 | strtab = (char*)section->d_un.d_ptr; | |
| 42 | } | ||
| 43 |
2/2✓ Branch 0 (19→20) taken 1 times.
✓ Branch 1 (19→21) taken 28 times.
|
29 | if(section->d_tag == DT_SYMENT){ |
| 44 | 1 | symentries = section->d_un.d_val; | |
| 45 | } | ||
| 46 | } | ||
| 47 | 1 | int size = strtab - (char *)symtab; | |
| 48 |
2/2✓ Branch 0 (33→24) taken 49 times.
✓ Branch 1 (33→34) taken 1 times.
|
50 | for(int k = 0; k < size / symentries; ++k){ |
| 49 | 49 | auto sym = &symtab[k]; | |
| 50 | // If sym is function | ||
| 51 |
2/2✓ Branch 0 (24→25) taken 37 times.
✓ Branch 1 (24→32) taken 12 times.
|
49 | if(ELF64_ST_TYPE(symtab[k].st_info) == STT_FUNC){ |
| 52 | //str is name of each symbol | ||
| 53 | 37 | auto str = &strtab[sym->st_name]; | |
| 54 |
2/2✓ Branch 0 (27→28) taken 37 times.
✓ Branch 2 (28→29) taken 37 times.
|
74 | vecFunction.push_back(std::string(str)); |
| 55 | // printf("%s\n", str); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | 1 | dlclose(library); | |
| 59 | } | ||
| 60 | |||
| 61 | ///Mangle the given function prototype | ||
| 62 | /** @param functionPrototype : prototype of the function to be mangled | ||
| 63 | * @param extraInclude : extra include to be used to get the mangled function | ||
| 64 | * @param compiler : compiler to be used | ||
| 65 | * @return corresponding mangled function name | ||
| 66 | */ | ||
| 67 | 2 | std::string phoenix_mangleFunction(const std::string & functionPrototype, const std::string & extraInclude, const std::string & compiler){ | |
| 68 |
6/6✓ Branch 0 (2→3) taken 2 times.
✓ Branch 2 (3→4) taken 2 times.
✓ Branch 4 (4→5) taken 2 times.
✓ Branch 6 (5→6) taken 2 times.
✓ Branch 8 (6→7) taken 2 times.
✓ Branch 10 (7→8) taken 2 times.
|
2 | std::string command("echo \"#include <new>\\n#include <string>\\n"+extraInclude+"\\n"+functionPrototype+" {} \" | "+compiler+" -x c++ -S - -o- | grep \"@function\" | cut -d ',' -f 1 | sed -e \"s/.type//g\" | sed -s \"s/ //g\" | sed -e \"s/\t//g\""); |
| 69 |
1/1✓ Branch 0 (14→15) taken 2 times.
|
2 | FILE * fp = popen(command.c_str(), "r"); |
| 70 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→25) taken 2 times.
|
2 | if(fp == NULL){ |
| 71 | ✗ | std::cerr << "phoenix_mangleFunction : cannot get result of command '"<<command<<"'" << std::endl; | |
| 72 | ✗ | return ""; | |
| 73 | } | ||
| 74 |
1/1✓ Branch 0 (27→28) taken 2 times.
|
2 | std::string mangledFunction(""); |
| 75 | int buffer; | ||
| 76 |
1/2✓ Branch 0 (39→30) taken 71 times.
✗ Branch 1 (39→40) not taken.
|
71 | while(!feof(fp)){ |
| 77 |
1/1✓ Branch 0 (30→31) taken 71 times.
|
71 | buffer = fgetc(fp); |
| 78 |
2/2✓ Branch 0 (31→32) taken 69 times.
✓ Branch 1 (31→36) taken 2 times.
|
71 | if(buffer != EOF){ |
| 79 | 69 | char ch = (char)buffer; | |
| 80 |
4/6✓ Branch 0 (32→33) taken 69 times.
✗ Branch 1 (32→37) not taken.
✓ Branch 2 (33→34) taken 69 times.
✗ Branch 3 (33→37) not taken.
✓ Branch 4 (34→35) taken 68 times.
✓ Branch 5 (34→37) taken 1 times.
|
69 | if(ch != ' ' && ch != '\t' && ch != '\n'){ |
| 81 |
1/1✓ Branch 0 (35→37) taken 68 times.
|
68 | mangledFunction += ch; |
| 82 | } | ||
| 83 | } | ||
| 84 | 2 | else break; | |
| 85 | } | ||
| 86 |
1/1✓ Branch 0 (40→41) taken 2 times.
|
2 | pclose(fp); |
| 87 | 2 | return mangledFunction; | |
| 88 | 2 | } | |
| 89 | |||
| 90 | |||
| 91 |