• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef COMMON_H
2 #define COMMON_H
3 
4 #include <libelf.h>
5 #include <elf.h>
6 
7 #define unlikely(expr) __builtin_expect (expr, 0)
8 #define likely(expr)   __builtin_expect (expr, 1)
9 
10 #define MIN(a,b) ((a)<(b)?(a):(b)) /* no side effects in arguments allowed! */
11 
12 typedef int (*section_match_fn_t)(Elf *, Elf_Scn *, void *);
13 void map_over_sections(Elf *, section_match_fn_t, void *);
14 
15 typedef int (*segment_match_fn_t)(Elf *, Elf32_Phdr *, void *);
16 void map_over_segments(Elf *, segment_match_fn_t, void *);
17 
18 typedef struct {
19     Elf_Scn *sect;
20     Elf32_Shdr *hdr;
21     Elf_Data *data;
22     size_t index;
23 } section_info_t;
24 
get_section_info(Elf_Scn * sect,section_info_t * info)25 static inline void get_section_info(Elf_Scn *sect, section_info_t *info)
26 {
27     info->sect = sect;
28     info->data = elf_getdata(sect, 0);
29     info->hdr = elf32_getshdr(sect);
30     info->index = elf_ndxscn(sect);
31 }
32 
is_host_little(void)33 static inline int is_host_little(void)
34 {
35     short val = 0x10;
36     return ((char *)&val)[0] != 0;
37 }
38 
switch_endianness(long val)39 static inline long switch_endianness(long val)
40 {
41 	long newval;
42 	((char *)&newval)[3] = ((char *)&val)[0];
43 	((char *)&newval)[2] = ((char *)&val)[1];
44 	((char *)&newval)[1] = ((char *)&val)[2];
45 	((char *)&newval)[0] = ((char *)&val)[3];
46 	return newval;
47 }
48 
49 #endif/*COMMON_H*/
50