blob: dacf930c68f308b0d76c9d0e3835bbcdc51f2a9a [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001#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
12typedef int (*section_match_fn_t)(Elf *, Elf_Scn *, void *);
13void map_over_sections(Elf *, section_match_fn_t, void *);
14
15typedef int (*segment_match_fn_t)(Elf *, Elf32_Phdr *, void *);
16void map_over_segments(Elf *, segment_match_fn_t, void *);
17
18typedef struct {
19 Elf_Scn *sect;
20 Elf32_Shdr *hdr;
21 Elf_Data *data;
22 size_t index;
23} section_info_t;
24
25static 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
33static inline int is_host_little(void)
34{
35 short val = 0x10;
36 return ((char *)&val)[0] != 0;
37}
38
39static 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*/