Skip to content

Commit

Permalink
Merge 3d0f99d into e700c00
Browse files Browse the repository at this point in the history
  • Loading branch information
hariharan-devarajan committed Sep 14, 2023
2 parents e700c00 + 3d0f99d commit f7c3f27
Show file tree
Hide file tree
Showing 77 changed files with 3,326 additions and 3,406 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BasedOnStyle: Google
14 changes: 12 additions & 2 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,28 @@ jobs:
- name: Push checkout
if: github.event_name == 'push'
uses: actions/checkout@v3

with:
fetch-depth: 2
- name: PR checkout
if: github.event_name == 'pull_request'
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 2

- name: Install additional packages
run: |
sudo apt-get update
sudo apt-get install cmake gcovr check
sudo apt-get install cmake gcovr check clang-format
- name: Check code formatting
if: matrix.os == 'ubuntu-22.04'
run: |
git clang-format --diff HEAD~1 -q
if git clang-format --diff HEAD~1 -q | grep -m 1 '^' >/dev/null; then
echo >&2 "Failed clang-format check. Run: git clang-format HEAD~1"
exit 1
fi
- name: Configure and Build
run: |
cmake . -B build -DGOTCHA_ENABLE_TESTS=ON -DCMAKE_BUILD_TYPE=PROFILE -DGOTCHA_ENABLE_WARNING_ERROR=ON
Expand Down
42 changes: 24 additions & 18 deletions include/gotcha/gotcha.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The intended use pattern is as follows
*
* TODO ON-INTERFACE-SOLID: document the interface
* usage
* TODO ON-INTERFACE-SOLID: document the
*interface usage
*
******************************************************************************
*/
#ifndef GOTCHA_H
#define GOTCHA_H

#include "gotcha/gotcha_types.h"
#include <link.h>

#if defined(__cplusplus)
#include "gotcha/gotcha_types.h"

#if defined(__cplusplus)
extern "C" {
#endif

Expand All @@ -45,19 +46,20 @@ extern "C" {
* \param name The name of the function you want to get a pointer to
* \param ret_type The return type of the function you want a pointer to
* \param ... A comma separated list of the types of the parameters
* to the function you're getting a pointer to
* to the function you're getting a pointer to
******************************************************************************
*/

#define GOTCHA_MAKE_FUNCTION_PTR(name, ret_type, ...) ret_type(*name)(__VA_ARGS__)
#define GOTCHA_MAKE_FUNCTION_PTR(name, ret_type, ...) \
ret_type (*name)(__VA_ARGS__)

#define GOTCHA_EXPORT __attribute__((__visibility__("default")))

/*!
******************************************************************************
*
* \fn enum gotcha_error_t gotcha_wrap(struct gotcha_binding_t* bindings,
* void** wrappers, void*** originals,
* \fn enum gotcha_error_t gotcha_wrap(struct gotcha_binding_t* bindings,
* void** wrappers, void*** originals,
* int num_actions);
*
* \brief Makes GOTCHA wrap the functions picked in gotcha_prepare_symbols
Expand All @@ -70,8 +72,9 @@ extern "C" {
******************************************************************************
*/

GOTCHA_EXPORT enum gotcha_error_t gotcha_wrap(struct gotcha_binding_t* bindings, int num_actions, const char* tool_name);

GOTCHA_EXPORT enum gotcha_error_t gotcha_wrap(struct gotcha_binding_t *bindings,
int num_actions,
const char *tool_name);

/*!
******************************************************************************
Expand All @@ -88,7 +91,8 @@ GOTCHA_EXPORT enum gotcha_error_t gotcha_wrap(struct gotcha_binding_t* bindings,
*
******************************************************************************
*/
GOTCHA_EXPORT enum gotcha_error_t gotcha_set_priority(const char* tool_name, int priority);
GOTCHA_EXPORT enum gotcha_error_t gotcha_set_priority(const char *tool_name,
int priority);

/*!
******************************************************************************
Expand All @@ -104,28 +108,30 @@ GOTCHA_EXPORT enum gotcha_error_t gotcha_set_priority(const char* tool_name, int
*
******************************************************************************
*/
GOTCHA_EXPORT enum gotcha_error_t gotcha_get_priority(const char* tool_name, int *priority);
GOTCHA_EXPORT enum gotcha_error_t gotcha_get_priority(const char *tool_name,
int *priority);

/*!
******************************************************************************
*
* \fn enum void* gotcha_get_wrappee(gotcha_wrappee_handle_t)
*
* \brief Given a GOTCHA wrapper's handle, returns the wrapped function for it to call
* \brief Given a GOTCHA wrapper's handle, returns the wrapped function for it
*to call
*
* \param handle The wrappee handle to return the function pointer for
*
******************************************************************************
*/
GOTCHA_EXPORT void* gotcha_get_wrappee(gotcha_wrappee_handle_t handle);
GOTCHA_EXPORT void *gotcha_get_wrappee(gotcha_wrappee_handle_t handle);

GOTCHA_EXPORT void gotcha_filter_libraries_by_name(const char* nameFilter);
GOTCHA_EXPORT void gotcha_filter_libraries_by_name(const char *nameFilter);
GOTCHA_EXPORT void gotcha_only_filter_last();
GOTCHA_EXPORT void gotcha_set_library_filter_func(int(*new_func)(struct link_map*));
GOTCHA_EXPORT void gotcha_set_library_filter_func(
int (*new_func)(struct link_map *));
GOTCHA_EXPORT void gotcha_restore_library_filter_func();


#if defined(__cplusplus)
#if defined(__cplusplus)
}
#endif

Expand Down
26 changes: 14 additions & 12 deletions include/gotcha/gotcha_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,35 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef GOTCHA_TYPES_H
#define GOTCHA_TYPES_H

#if defined(__cplusplus)
#if defined(__cplusplus)
extern "C" {
#endif

typedef void* gotcha_wrappee_handle_t;
typedef void *gotcha_wrappee_handle_t;

/*!
* The representation of a Gotcha action
* as it passes through the pipeline
*/
typedef struct gotcha_binding_t {
const char* name; //!< The name of the function being wrapped
void* wrapper_pointer; //!< A pointer to the wrapper function
gotcha_wrappee_handle_t* function_handle; //!< A pointer to the function being wrapped
}gotcha_binding_t;
const char *name; //!< The name of the function being wrapped
void *wrapper_pointer; //!< A pointer to the wrapper function
gotcha_wrappee_handle_t
*function_handle; //!< A pointer to the function being wrapped
} gotcha_binding_t;

/*!
* The representation of an error (or success) of a Gotcha action
*/
typedef enum gotcha_error_t {
GOTCHA_SUCCESS = 0, //!< The call succeeded
GOTCHA_FUNCTION_NOT_FOUND, //!< The call looked up a function which could not be found
GOTCHA_INTERNAL, //!< Internal gotcha error
GOTCHA_INVALID_TOOL //!< Invalid tool name
}gotcha_error_t;
GOTCHA_SUCCESS = 0, //!< The call succeeded
GOTCHA_FUNCTION_NOT_FOUND, //!< The call looked up a function which could not
//!< be found
GOTCHA_INTERNAL, //!< Internal gotcha error
GOTCHA_INVALID_TOOL //!< Invalid tool name
} gotcha_error_t;

#if defined(__cplusplus)
#if defined(__cplusplus)
}
#endif

Expand Down
46 changes: 23 additions & 23 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
include(GNUInstallDirs)
set(GOTCHA_SOURCES
gotcha_utils.c
gotcha.c
gotcha_auxv.c
libc_wrappers.c
elf_ops.c
hash.c
tool.c
library_filters.c
gotcha_dl.c
translations.c
gotcha_utils.c
gotcha.c
gotcha_auxv.c
libc_wrappers.c
elf_ops.c
hash.c
tool.c
library_filters.c
gotcha_dl.c
translations.c
)

add_library(gotcha SHARED ${GOTCHA_SOURCES})
Expand All @@ -18,25 +18,25 @@ add_library(gotcha SHARED ${GOTCHA_SOURCES})
set_target_properties(gotcha PROPERTIES SOVERSION ${LIBTOOL_INTERFACE})
set_target_properties(gotcha PROPERTIES VERSION "${LIBTOOL_INTERFACE}.${LIBTOOL_REVISION}.${LIBTOOL_AGE}")
set_target_properties(gotcha PROPERTIES COMPILE_FLAGS "-fvisibility=${DEFAULT_SYMBOL_VISIBILITY}")
if(GOTCHA_ENABLE_WARNING_ERROR)
if (GOTCHA_ENABLE_WARNING_ERROR)
set_target_properties(gotcha PROPERTIES COMPILE_FLAGS "-Wall -Werror")
endif()
endif ()
set_target_properties(gotcha PROPERTIES LINK_FLAGS "-fvisibility=${DEFAULT_SYMBOL_VISIBILITY}")
target_include_directories(gotcha PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
if(GOTCHA_ENABLE_TESTS)
add_library(gotcha_no_libc SHARED ${GOTCHA_SOURCES})
set_target_properties(gotcha_no_libc PROPERTIES SOVERSION ${LIBTOOL_INTERFACE})
set_target_properties(gotcha_no_libc PROPERTIES VERSION "${LIBTOOL_INTERFACE}.${LIBTOOL_REVISION}.${LIBTOOL_AGE}")
set_target_properties(gotcha_no_libc PROPERTIES COMPILE_DEFINITIONS "FORCE_NO_LIBC")
set_target_properties(gotcha_no_libc PROPERTIES COMPILE_FLAGS "-fvisibility=${DEFAULT_SYMBOL_VISIBILITY}")
set_target_properties(gotcha_no_libc PROPERTIES LINK_FLAGS "-fvisibility=${DEFAULT_SYMBOL_VISIBILITY}")
endif()
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
if (GOTCHA_ENABLE_TESTS)
add_library(gotcha_no_libc SHARED ${GOTCHA_SOURCES})
set_target_properties(gotcha_no_libc PROPERTIES SOVERSION ${LIBTOOL_INTERFACE})
set_target_properties(gotcha_no_libc PROPERTIES VERSION "${LIBTOOL_INTERFACE}.${LIBTOOL_REVISION}.${LIBTOOL_AGE}")
set_target_properties(gotcha_no_libc PROPERTIES COMPILE_DEFINITIONS "FORCE_NO_LIBC")
set_target_properties(gotcha_no_libc PROPERTIES COMPILE_FLAGS "-fvisibility=${DEFAULT_SYMBOL_VISIBILITY}")
set_target_properties(gotcha_no_libc PROPERTIES LINK_FLAGS "-fvisibility=${DEFAULT_SYMBOL_VISIBILITY}")
endif ()

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

install(TARGETS gotcha EXPORT gotcha-targets DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS gotcha EXPORT gotcha-targets DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(EXPORT gotcha-targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/gotcha)

add_subdirectory(example)
24 changes: 13 additions & 11 deletions src/elf_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "elf_ops.h"
#include "libc_wrappers.h"

#include <elf.h>

#include "libc_wrappers.h"
struct gnu_hash_header {
uint32_t nbuckets; //!< The number of buckets to hash symbols into
uint32_t symndx; //!< Index of the first symbol accessible via hashtable in the symbol table
uint32_t maskwords; //!< Number of words in the hash table's bloom filter
uint32_t shift2; //!< The bloom filter's shift count
uint32_t nbuckets; //!< The number of buckets to hash symbols into
uint32_t symndx; //!< Index of the first symbol accessible via hashtable in
//!< the symbol table
uint32_t maskwords; //!< Number of words in the hash table's bloom filter
uint32_t shift2; //!< The bloom filter's shift count
};

static uint32_t gnu_hash_func(const char *str) {
Expand All @@ -43,15 +46,14 @@ static uint32_t gnu_hash_func(const char *str) {
*/

signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms,
ElfW(Half) *versym,
char *symnames,
ElfW(Half) * versym, char *symnames,
void *sheader) {
uint32_t *buckets, *vals;
uint32_t hash_val;
uint32_t cur_sym, cur_sym_hashval;
signed long latest_sym = -1;
ElfW(Half) latest_sym_ver = 0;
struct gnu_hash_header *header = (struct gnu_hash_header *) (sheader);
struct gnu_hash_header *header = (struct gnu_hash_header *)(sheader);

buckets = (uint32_t *)(((unsigned char *)(header + 1)) +
(header->maskwords * sizeof(ElfW(Addr))));
Expand Down Expand Up @@ -99,8 +101,8 @@ static unsigned long elf_hash(const unsigned char *name) {
}

signed long lookup_elf_hash_symbol(const char *name, ElfW(Sym) * syms,
ElfW(Half) *versym,
char *symnames, ElfW(Word) * header) {
ElfW(Half) * versym, char *symnames,
ElfW(Word) * header) {
ElfW(Word) *nbucket = header + 0;
ElfW(Word) *buckets = header + 2;
ElfW(Word) *chains = buckets + *nbucket;
Expand All @@ -113,7 +115,7 @@ signed long lookup_elf_hash_symbol(const char *name, ElfW(Sym) * syms,
if (gotcha_strcmp(name, symnames + syms[y].st_name) == 0) {
if (!versym) {
// in general all libs would have version but it is a guard condition.
return y; // GCOVR_EXCL_LINE
return y; // GCOVR_EXCL_LINE
}

if ((versym[y] & 0x7fff) > latest_sym_ver) {
Expand Down

0 comments on commit f7c3f27

Please sign in to comment.