xref: /qemu/bsd-user/openbsd/target_os_elf.h (revision 82723866)
198b34d35SWarner Losh /*
298b34d35SWarner Losh  *  openbsd ELF definitions
398b34d35SWarner Losh  *
498b34d35SWarner Losh  *  Copyright (c) 2013 Stacey D. Son
598b34d35SWarner Losh  *
698b34d35SWarner Losh  *  This program is free software; you can redistribute it and/or modify
798b34d35SWarner Losh  *  it under the terms of the GNU General Public License as published by
898b34d35SWarner Losh  *  the Free Software Foundation; either version 2 of the License, or
998b34d35SWarner Losh  *  (at your option) any later version.
1098b34d35SWarner Losh  *
1198b34d35SWarner Losh  *  This program is distributed in the hope that it will be useful,
1298b34d35SWarner Losh  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1398b34d35SWarner Losh  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1498b34d35SWarner Losh  *  GNU General Public License for more details.
1598b34d35SWarner Losh  *
1698b34d35SWarner Losh  *  You should have received a copy of the GNU General Public License
1798b34d35SWarner Losh  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
1898b34d35SWarner Losh  */
199c092804SMarkus Armbruster 
209c092804SMarkus Armbruster #ifndef TARGET_OS_ELF_H
219c092804SMarkus Armbruster #define TARGET_OS_ELF_H
2298b34d35SWarner Losh 
2398b34d35SWarner Losh #include "target_arch_elf.h"
2498b34d35SWarner Losh #include "elf.h"
2582723866SPhilippe Mathieu-Daudé #include "user/tswap-target.h"
2698b34d35SWarner Losh 
2798b34d35SWarner Losh /* this flag is uneffective under linux too, should be deleted */
2898b34d35SWarner Losh #ifndef MAP_DENYWRITE
2998b34d35SWarner Losh #define MAP_DENYWRITE 0
3098b34d35SWarner Losh #endif
3198b34d35SWarner Losh 
3298b34d35SWarner Losh /* should probably go in elf.h */
3398b34d35SWarner Losh #ifndef ELIBBAD
3498b34d35SWarner Losh #define ELIBBAD 80
3598b34d35SWarner Losh #endif
3698b34d35SWarner Losh 
3798b34d35SWarner Losh #ifndef ELF_PLATFORM
3898b34d35SWarner Losh #define ELF_PLATFORM (NULL)
3998b34d35SWarner Losh #endif
4098b34d35SWarner Losh 
4198b34d35SWarner Losh #ifndef ELF_HWCAP
4298b34d35SWarner Losh #define ELF_HWCAP 0
4398b34d35SWarner Losh #endif
4498b34d35SWarner Losh 
4598b34d35SWarner Losh #ifdef TARGET_ABI32
4698b34d35SWarner Losh #undef ELF_CLASS
4798b34d35SWarner Losh #define ELF_CLASS ELFCLASS32
4898b34d35SWarner Losh #undef bswaptls
4998b34d35SWarner Losh #define bswaptls(ptr) bswap32s(ptr)
5098b34d35SWarner Losh #endif
5198b34d35SWarner Losh 
5298b34d35SWarner Losh /* max code+data+bss space allocated to elf interpreter */
5398b34d35SWarner Losh #define INTERP_MAP_SIZE (32 * 1024 * 1024)
5498b34d35SWarner Losh 
5598b34d35SWarner Losh /* max code+data+bss+brk space allocated to ET_DYN executables */
5698b34d35SWarner Losh #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
5798b34d35SWarner Losh 
5898b34d35SWarner Losh /* Necessary parameters */
5998b34d35SWarner Losh #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
6098b34d35SWarner Losh #define TARGET_ELF_PAGESTART(_v) ((_v) & \
6198b34d35SWarner Losh         ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE - 1))
6298b34d35SWarner Losh #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE - 1))
6398b34d35SWarner Losh 
6498b34d35SWarner Losh #define DLINFO_ITEMS 12
6598b34d35SWarner Losh 
target_create_elf_tables(abi_ulong p,int argc,int envc,abi_ulong stringp,struct elfhdr * exec,abi_ulong load_addr,abi_ulong load_bias,abi_ulong interp_load_addr,struct image_info * info)6698b34d35SWarner Losh static abi_ulong target_create_elf_tables(abi_ulong p, int argc, int envc,
6798b34d35SWarner Losh                                           abi_ulong stringp,
6898b34d35SWarner Losh                                           struct elfhdr *exec,
6998b34d35SWarner Losh                                           abi_ulong load_addr,
7098b34d35SWarner Losh                                           abi_ulong load_bias,
7198b34d35SWarner Losh                                           abi_ulong interp_load_addr,
7298b34d35SWarner Losh                                           struct image_info *info)
7398b34d35SWarner Losh {
7498b34d35SWarner Losh         abi_ulong sp;
7598b34d35SWarner Losh         int size;
7698b34d35SWarner Losh         abi_ulong u_platform;
7798b34d35SWarner Losh         const char *k_platform;
7898b34d35SWarner Losh         const int n = sizeof(elf_addr_t);
7998b34d35SWarner Losh 
8098b34d35SWarner Losh         sp = p;
8198b34d35SWarner Losh         u_platform = 0;
8298b34d35SWarner Losh         k_platform = ELF_PLATFORM;
8398b34d35SWarner Losh         if (k_platform) {
8498b34d35SWarner Losh             size_t len = strlen(k_platform) + 1;
8598b34d35SWarner Losh             sp -= (len + n - 1) & ~(n - 1);
8698b34d35SWarner Losh             u_platform = sp;
8798b34d35SWarner Losh             /* FIXME - check return value of memcpy_to_target() for failure */
8898b34d35SWarner Losh             memcpy_to_target(sp, k_platform, len);
8998b34d35SWarner Losh         }
9098b34d35SWarner Losh         /*
9198b34d35SWarner Losh          * Force 16 byte _final_ alignment here for generality.
9298b34d35SWarner Losh          */
9398b34d35SWarner Losh         sp = sp & ~(abi_ulong)15;
9498b34d35SWarner Losh         size = (DLINFO_ITEMS + 1) * 2;
9598b34d35SWarner Losh         if (k_platform) {
9698b34d35SWarner Losh             size += 2;
9798b34d35SWarner Losh         }
9898b34d35SWarner Losh #ifdef DLINFO_ARCH_ITEMS
9998b34d35SWarner Losh         size += DLINFO_ARCH_ITEMS * 2;
10098b34d35SWarner Losh #endif
10198b34d35SWarner Losh         size += envc + argc + 2;
10298b34d35SWarner Losh         size += 1;                        /* argc itself */
10398b34d35SWarner Losh         size *= n;
10498b34d35SWarner Losh         if (size & 15) {
10598b34d35SWarner Losh             sp -= 16 - (size & 15);
10698b34d35SWarner Losh         }
10798b34d35SWarner Losh 
10898b34d35SWarner Losh         /*
10998b34d35SWarner Losh          * OpenBSD defines elf_addr_t as Elf32_Off / Elf64_Off
11098b34d35SWarner Losh          */
11198b34d35SWarner Losh #define NEW_AUX_ENT(id, val) do {               \
11298b34d35SWarner Losh             sp -= n; put_user_ual(val, sp);     \
11398b34d35SWarner Losh             sp -= n; put_user_ual(id, sp);      \
11498b34d35SWarner Losh           } while (0)
11598b34d35SWarner Losh 
11698b34d35SWarner Losh         NEW_AUX_ENT(AT_NULL, 0);
11798b34d35SWarner Losh 
11898b34d35SWarner Losh         /* There must be exactly DLINFO_ITEMS entries here.  */
11998b34d35SWarner Losh         NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
12098b34d35SWarner Losh         NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof(struct elf_phdr)));
12198b34d35SWarner Losh         NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
12298b34d35SWarner Losh         NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
12398b34d35SWarner Losh         NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
12498b34d35SWarner Losh         NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
12598b34d35SWarner Losh         NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
12698b34d35SWarner Losh         NEW_AUX_ENT(AT_UID, (abi_ulong)getuid());
12798b34d35SWarner Losh         NEW_AUX_ENT(AT_EUID, (abi_ulong)geteuid());
12898b34d35SWarner Losh         NEW_AUX_ENT(AT_GID, (abi_ulong)getgid());
12998b34d35SWarner Losh         NEW_AUX_ENT(AT_EGID, (abi_ulong)getegid());
13098b34d35SWarner Losh         NEW_AUX_ENT(AT_HWCAP, (abi_ulong)ELF_HWCAP);
13198b34d35SWarner Losh         NEW_AUX_ENT(AT_CLKTCK, (abi_ulong)sysconf(_SC_CLK_TCK));
13298b34d35SWarner Losh         if (k_platform) {
13398b34d35SWarner Losh             NEW_AUX_ENT(AT_PLATFORM, u_platform);
13498b34d35SWarner Losh         }
13598b34d35SWarner Losh #ifdef ARCH_DLINFO
13698b34d35SWarner Losh         /*
13798b34d35SWarner Losh          * ARCH_DLINFO must come last so platform specific code can enforce
13898b34d35SWarner Losh          * special alignment requirements on the AUXV if necessary (eg. PPC).
13998b34d35SWarner Losh          */
14098b34d35SWarner Losh         ARCH_DLINFO;
14198b34d35SWarner Losh #endif
14298b34d35SWarner Losh #undef NEW_AUX_ENT
14398b34d35SWarner Losh 
14498b34d35SWarner Losh         sp = loader_build_argptr(envc, argc, sp, stringp);
14598b34d35SWarner Losh         return sp;
14698b34d35SWarner Losh }
14798b34d35SWarner Losh 
1489c092804SMarkus Armbruster #endif /* TARGET_OS_ELF_H */
149