1 /*
2  *  openbsd ELF definitions
3  *
4  *  Copyright (c) 2013 Stacey D. Son
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TARGET_OS_ELF_H
21 #define TARGET_OS_ELF_H
22 
23 #include "target_arch_elf.h"
24 #include "elf.h"
25 #include "user/tswap-target.h"
26 
27 /* this flag is uneffective under linux too, should be deleted */
28 #ifndef MAP_DENYWRITE
29 #define MAP_DENYWRITE 0
30 #endif
31 
32 /* should probably go in elf.h */
33 #ifndef ELIBBAD
34 #define ELIBBAD 80
35 #endif
36 
37 #ifndef ELF_PLATFORM
38 #define ELF_PLATFORM (NULL)
39 #endif
40 
41 #ifndef ELF_HWCAP
42 #define ELF_HWCAP 0
43 #endif
44 
45 #ifdef TARGET_ABI32
46 #undef ELF_CLASS
47 #define ELF_CLASS ELFCLASS32
48 #undef bswaptls
49 #define bswaptls(ptr) bswap32s(ptr)
50 #endif
51 
52 /* max code+data+bss space allocated to elf interpreter */
53 #define INTERP_MAP_SIZE (32 * 1024 * 1024)
54 
55 /* max code+data+bss+brk space allocated to ET_DYN executables */
56 #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
57 
58 /* Necessary parameters */
59 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
60 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
61         ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE - 1))
62 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE - 1))
63 
64 #define DLINFO_ITEMS 12
65 
66 static abi_ulong target_create_elf_tables(abi_ulong p, int argc, int envc,
67                                           abi_ulong stringp,
68                                           struct elfhdr *exec,
69                                           abi_ulong load_addr,
70                                           abi_ulong load_bias,
71                                           abi_ulong interp_load_addr,
72                                           struct image_info *info)
73 {
74         abi_ulong sp;
75         int size;
76         abi_ulong u_platform;
77         const char *k_platform;
78         const int n = sizeof(elf_addr_t);
79 
80         sp = p;
81         u_platform = 0;
82         k_platform = ELF_PLATFORM;
83         if (k_platform) {
84             size_t len = strlen(k_platform) + 1;
85             sp -= (len + n - 1) & ~(n - 1);
86             u_platform = sp;
87             /* FIXME - check return value of memcpy_to_target() for failure */
88             memcpy_to_target(sp, k_platform, len);
89         }
90         /*
91          * Force 16 byte _final_ alignment here for generality.
92          */
93         sp = sp & ~(abi_ulong)15;
94         size = (DLINFO_ITEMS + 1) * 2;
95         if (k_platform) {
96             size += 2;
97         }
98 #ifdef DLINFO_ARCH_ITEMS
99         size += DLINFO_ARCH_ITEMS * 2;
100 #endif
101         size += envc + argc + 2;
102         size += 1;                        /* argc itself */
103         size *= n;
104         if (size & 15) {
105             sp -= 16 - (size & 15);
106         }
107 
108         /*
109          * OpenBSD defines elf_addr_t as Elf32_Off / Elf64_Off
110          */
111 #define NEW_AUX_ENT(id, val) do {               \
112             sp -= n; put_user_ual(val, sp);     \
113             sp -= n; put_user_ual(id, sp);      \
114           } while (0)
115 
116         NEW_AUX_ENT(AT_NULL, 0);
117 
118         /* There must be exactly DLINFO_ITEMS entries here.  */
119         NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
120         NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof(struct elf_phdr)));
121         NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
122         NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
123         NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
124         NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
125         NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
126         NEW_AUX_ENT(AT_UID, (abi_ulong)getuid());
127         NEW_AUX_ENT(AT_EUID, (abi_ulong)geteuid());
128         NEW_AUX_ENT(AT_GID, (abi_ulong)getgid());
129         NEW_AUX_ENT(AT_EGID, (abi_ulong)getegid());
130         NEW_AUX_ENT(AT_HWCAP, (abi_ulong)ELF_HWCAP);
131         NEW_AUX_ENT(AT_CLKTCK, (abi_ulong)sysconf(_SC_CLK_TCK));
132         if (k_platform) {
133             NEW_AUX_ENT(AT_PLATFORM, u_platform);
134         }
135 #ifdef ARCH_DLINFO
136         /*
137          * ARCH_DLINFO must come last so platform specific code can enforce
138          * special alignment requirements on the AUXV if necessary (eg. PPC).
139          */
140         ARCH_DLINFO;
141 #endif
142 #undef NEW_AUX_ENT
143 
144         sp = loader_build_argptr(envc, argc, sp, stringp);
145         return sp;
146 }
147 
148 #endif /* TARGET_OS_ELF_H */
149