GPLv2 release
[centaur.git] / include / libelfu / generic.h
1 /* This file is part of centaur.
2  *
3  * centaur is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License 2 as
5  * published by the Free Software Foundation.
6
7  * centaur is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
14  */
15
16 #ifndef __LIBELFU_GENERIC_H__
17 #define __LIBELFU_GENERIC_H__
18
19 #include <gelf.h>
20
21
22 #ifndef MIN
23   #define MIN(x, y) ((x) < (y) ? (x) : (y))
24 #endif
25
26 #ifndef MAX
27   #define MAX(x, y) ((x) > (y) ? (x) : (y))
28 #endif
29
30 #ifndef ROUNDUP
31   #define ROUNDUP(x, align) ((x) + ((align) - ((x) % (align))) % (align))
32 #endif
33
34
35 #define OFFS_END(off, sz) ((off) + (sz))
36
37 #define OVERLAPPING(off1, sz1, off2, sz2) \
38   (!((off1) == (off2) && ((sz1 == 0) || (sz2 == 0))) \
39    && (((off1) <= (off2) && (off2) < OFFS_END((off1), (sz1))) \
40        || ((off2) <= (off1) && (off1) < OFFS_END((off2), (sz2)))) \
41   )
42
43 #define FULLY_OVERLAPPING(off1, sz1, off2, sz2) \
44   (((off1) <= (off2) && OFFS_END((off2), (sz2)) <= OFFS_END((off1), (sz1))) \
45    || ((off2) <= (off1) && OFFS_END((off1), (sz1)) <= OFFS_END((off2), (sz2))))
46
47
48
49 #define SCNFILESIZE(shdr) ((shdr)->sh_type == SHT_NOBITS ? 0 : (shdr)->sh_size)
50
51
52
53 #define PHDR_CONTAINS_SCN_IN_MEMORY(phdr, shdr) \
54   (((phdr)->p_vaddr) <= ((shdr)->sh_addr) \
55    && OFFS_END((shdr)->sh_addr, (shdr)->sh_size) <= OFFS_END((phdr)->p_vaddr, (phdr)->p_memsz))
56
57 #define PHDR_CONTAINS_SCN_IN_FILE(phdr, shdr) \
58   (((phdr)->p_offset) <= ((shdr)->sh_offset) \
59    && OFFS_END((shdr)->sh_offset, SCNFILESIZE(shdr)) <= OFFS_END((phdr)->p_offset, (phdr)->p_filesz))
60
61
62 #endif