xref: /qemu/bsd-user/freebsd/target_os_elf.h (revision 82723866)
198b34d35SWarner Losh /*
298b34d35SWarner Losh  *  freebsd ELF definitions
398b34d35SWarner Losh  *
498b34d35SWarner Losh  *  Copyright (c) 2013-15 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 #define bsd_get_ncpu() 1 /* until we pull in bsd-proc.[hc] */
2898b34d35SWarner Losh 
2998b34d35SWarner Losh /* this flag is uneffective under linux too, should be deleted */
3098b34d35SWarner Losh #ifndef MAP_DENYWRITE
3198b34d35SWarner Losh #define MAP_DENYWRITE 0
3298b34d35SWarner Losh #endif
3398b34d35SWarner Losh 
3498b34d35SWarner Losh /* should probably go in elf.h */
3598b34d35SWarner Losh #ifndef ELIBBAD
3698b34d35SWarner Losh #define ELIBBAD 80
3798b34d35SWarner Losh #endif
3898b34d35SWarner Losh 
3998b34d35SWarner Losh #ifndef ELF_PLATFORM
4098b34d35SWarner Losh #define ELF_PLATFORM (NULL)
4198b34d35SWarner Losh #endif
4298b34d35SWarner Losh 
4398b34d35SWarner Losh /* XXX Look at the other conflicting AT_* values. */
4498b34d35SWarner Losh #define FREEBSD_AT_NCPUS     19
4598b34d35SWarner Losh #define FREEBSD_AT_HWCAP     25
4698b34d35SWarner Losh #define FREEBSD_AT_HWCAP2    26
4798b34d35SWarner Losh 
4898b34d35SWarner Losh #ifdef TARGET_ABI32
4998b34d35SWarner Losh #undef ELF_CLASS
5098b34d35SWarner Losh #define ELF_CLASS ELFCLASS32
5198b34d35SWarner Losh #undef bswaptls
5298b34d35SWarner Losh #define bswaptls(ptr) bswap32s(ptr)
5398b34d35SWarner Losh #endif
5498b34d35SWarner Losh 
5598b34d35SWarner Losh /* max code+data+bss space allocated to elf interpreter */
5698b34d35SWarner Losh #define INTERP_MAP_SIZE (32 * 1024 * 1024)
5798b34d35SWarner Losh 
5898b34d35SWarner Losh /* max code+data+bss+brk space allocated to ET_DYN executables */
5998b34d35SWarner Losh #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
6098b34d35SWarner Losh 
6198b34d35SWarner Losh /* Necessary parameters */
6298b34d35SWarner Losh #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
6398b34d35SWarner Losh #define TARGET_ELF_PAGESTART(_v) ((_v) & \
6498b34d35SWarner Losh         ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE - 1))
6598b34d35SWarner Losh #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE - 1))
6698b34d35SWarner Losh 
6798b34d35SWarner Losh #define DLINFO_ITEMS 14
6898b34d35SWarner 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)6998b34d35SWarner Losh static abi_ulong target_create_elf_tables(abi_ulong p, int argc, int envc,
7098b34d35SWarner Losh                                           abi_ulong stringp,
7198b34d35SWarner Losh                                           struct elfhdr *exec,
7298b34d35SWarner Losh                                           abi_ulong load_addr,
7398b34d35SWarner Losh                                           abi_ulong load_bias,
7498b34d35SWarner Losh                                           abi_ulong interp_load_addr,
7598b34d35SWarner Losh                                           struct image_info *info)
7698b34d35SWarner Losh {
7798b34d35SWarner Losh         abi_ulong features, sp;
7898b34d35SWarner Losh         int size;
7998b34d35SWarner Losh         const int n = sizeof(elf_addr_t);
8098b34d35SWarner Losh 
8198b34d35SWarner Losh         target_auxents_sz = 0;
8298b34d35SWarner Losh         sp = p;
8398b34d35SWarner Losh         /*
8498b34d35SWarner Losh          * Force 16 byte _final_ alignment here for generality.
8598b34d35SWarner Losh          */
8698b34d35SWarner Losh         sp = sp & ~(abi_ulong)15;
8798b34d35SWarner Losh         size = (DLINFO_ITEMS + 1) * 2;
8898b34d35SWarner Losh         size += envc + argc + 2;
8998b34d35SWarner Losh         size += 1;                      /* argc itself */
9098b34d35SWarner Losh         size *= n;
9198b34d35SWarner Losh         if (size & 15) {
9298b34d35SWarner Losh             sp -= 16 - (size & 15);
9398b34d35SWarner Losh         }
9498b34d35SWarner Losh 
9598b34d35SWarner Losh         /*
9698b34d35SWarner Losh          * FreeBSD defines elf_addr_t as Elf32_Off / Elf64_Off
9798b34d35SWarner Losh          */
9898b34d35SWarner Losh #define NEW_AUX_ENT(id, val) do {               \
9998b34d35SWarner Losh             sp -= n; put_user_ual(val, sp);     \
10098b34d35SWarner Losh             sp -= n; put_user_ual(id, sp);      \
10198b34d35SWarner Losh             target_auxents_sz += 2 * n;         \
10298b34d35SWarner Losh           } while (0)
10398b34d35SWarner Losh 
10498b34d35SWarner Losh         NEW_AUX_ENT(AT_NULL, 0);
10598b34d35SWarner Losh 
10698b34d35SWarner Losh         /* There must be exactly DLINFO_ITEMS entries here.  */
10798b34d35SWarner Losh         NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
10898b34d35SWarner Losh         NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof(struct elf_phdr)));
10998b34d35SWarner Losh         NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
11098b34d35SWarner Losh         NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
11198b34d35SWarner Losh         NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
11298b34d35SWarner Losh         NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
11398b34d35SWarner Losh         NEW_AUX_ENT(FREEBSD_AT_NCPUS, (abi_ulong)bsd_get_ncpu());
11498b34d35SWarner Losh         NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
11511170cbdSWarner Losh         features = ELF_HWCAP;
11611170cbdSWarner Losh         NEW_AUX_ENT(FREEBSD_AT_HWCAP, features);
117f6f0706cSWarner Losh #ifdef ELF_HWCAP2
118f6f0706cSWarner Losh         features = ELF_HWCAP2;
119f6f0706cSWarner Losh         NEW_AUX_ENT(FREEBSD_AT_HWCAP2, features);
120f6f0706cSWarner Losh #endif
12198b34d35SWarner Losh         NEW_AUX_ENT(AT_UID, (abi_ulong)getuid());
12298b34d35SWarner Losh         NEW_AUX_ENT(AT_EUID, (abi_ulong)geteuid());
12398b34d35SWarner Losh         NEW_AUX_ENT(AT_GID, (abi_ulong)getgid());
12498b34d35SWarner Losh         NEW_AUX_ENT(AT_EGID, (abi_ulong)getegid());
12598b34d35SWarner Losh         target_auxents = sp; /* Note where the aux entries are in the target */
12698b34d35SWarner Losh #ifdef ARCH_DLINFO
12798b34d35SWarner Losh         /*
12898b34d35SWarner Losh          * ARCH_DLINFO must come last so platform specific code can enforce
12998b34d35SWarner Losh          * special alignment requirements on the AUXV if necessary (eg. PPC).
13098b34d35SWarner Losh          */
13198b34d35SWarner Losh         ARCH_DLINFO;
13298b34d35SWarner Losh #endif
13398b34d35SWarner Losh #undef NEW_AUX_ENT
13498b34d35SWarner Losh 
13598b34d35SWarner Losh         sp = loader_build_argptr(envc, argc, sp, stringp);
13698b34d35SWarner Losh         return sp;
13798b34d35SWarner Losh }
13898b34d35SWarner Losh 
1399c092804SMarkus Armbruster #endif /* TARGET_OS_ELF_H */
140