1 /* This file is part of centaur.
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.
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.
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/>.
16 #ifndef __LIBELFU_GENERIC_H__
17 #define __LIBELFU_GENERIC_H__
23 #define MIN(x, y) ((x) < (y) ? (x) : (y))
27 #define MAX(x, y) ((x) > (y) ? (x) : (y))
31 #define ROUNDUP(x, align) ((x) + ((align) - ((x) % (align))) % (align))
35 #define OFFS_END(off, sz) ((off) + (sz))
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)))) \
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))))
49 #define SCNFILESIZE(shdr) ((shdr)->sh_type == SHT_NOBITS ? 0 : (shdr)->sh_size)
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))
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))