1 // SPDX-License-Identifier: GPL-2.0-only
3 * a.out loader for x86-64
5 * Copyright (C) 1991, 1992, 1996 Linus Torvalds
6 * Hacked together by Andi Kleen
9 #include <linux/module.h>
11 #include <linux/time.h>
12 #include <linux/kernel.h>
14 #include <linux/mman.h>
15 #include <linux/a.out.h>
16 #include <linux/errno.h>
17 #include <linux/signal.h>
18 #include <linux/string.h>
20 #include <linux/file.h>
21 #include <linux/stat.h>
22 #include <linux/fcntl.h>
23 #include <linux/ptrace.h>
24 #include <linux/user.h>
25 #include <linux/binfmts.h>
26 #include <linux/personality.h>
27 #include <linux/init.h>
28 #include <linux/jiffies.h>
29 #include <linux/perf_event.h>
30 #include <linux/sched/task_stack.h>
32 #include <linux/uaccess.h>
33 #include <asm/pgalloc.h>
34 #include <asm/cacheflush.h>
35 #include <asm/user32.h>
40 static int load_aout_binary(struct linux_binprm
*);
41 static int load_aout_library(struct file
*);
43 static struct linux_binfmt aout_format
= {
44 .module
= THIS_MODULE
,
45 .load_binary
= load_aout_binary
,
46 .load_shlib
= load_aout_library
,
49 static int set_brk(unsigned long start
, unsigned long end
)
51 start
= PAGE_ALIGN(start
);
52 end
= PAGE_ALIGN(end
);
55 return vm_brk(start
, end
- start
);
60 * create_aout_tables() parses the env- and arg-strings in new user
61 * memory and creates the pointer tables from them, and puts their
62 * addresses on the "stack", returning the new stack pointer value.
64 static u32 __user
*create_aout_tables(char __user
*p
, struct linux_binprm
*bprm
)
66 u32 __user
*argv
, *envp
, *sp
;
67 int argc
= bprm
->argc
, envc
= bprm
->envc
;
69 sp
= (u32 __user
*) ((-(unsigned long)sizeof(u32
)) & (unsigned long) p
);
74 put_user((unsigned long) envp
, --sp
);
75 put_user((unsigned long) argv
, --sp
);
77 current
->mm
->arg_start
= (unsigned long) p
;
81 put_user((u32
)(unsigned long)p
, argv
++);
87 current
->mm
->arg_end
= current
->mm
->env_start
= (unsigned long) p
;
91 put_user((u32
)(unsigned long)p
, envp
++);
97 current
->mm
->env_end
= (unsigned long) p
;
102 * These are the functions used to load a.out style executables and shared
103 * libraries. There is no binary dependent code anywhere else.
105 static int load_aout_binary(struct linux_binprm
*bprm
)
107 unsigned long error
, fd_offset
, rlim
;
108 struct pt_regs
*regs
= current_pt_regs();
112 ex
= *((struct exec
*) bprm
->buf
); /* exec-header */
113 if ((N_MAGIC(ex
) != ZMAGIC
&& N_MAGIC(ex
) != OMAGIC
&&
114 N_MAGIC(ex
) != QMAGIC
&& N_MAGIC(ex
) != NMAGIC
) ||
115 N_TRSIZE(ex
) || N_DRSIZE(ex
) ||
116 i_size_read(file_inode(bprm
->file
)) <
117 ex
.a_text
+ex
.a_data
+N_SYMSIZE(ex
)+N_TXTOFF(ex
)) {
121 fd_offset
= N_TXTOFF(ex
);
123 /* Check initial limits. This avoids letting people circumvent
124 * size limits imposed on them by creating programs with large
125 * arrays in the data or bss.
127 rlim
= rlimit(RLIMIT_DATA
);
128 if (rlim
>= RLIM_INFINITY
)
130 if (ex
.a_data
+ ex
.a_bss
> rlim
)
133 /* Flush all traces of the currently running executable */
134 retval
= flush_old_exec(bprm
);
138 /* OK, This is the point of no return */
139 set_personality(PER_LINUX
);
140 set_personality_ia32(false);
142 setup_new_exec(bprm
);
144 regs
->cs
= __USER32_CS
;
145 regs
->r8
= regs
->r9
= regs
->r10
= regs
->r11
= regs
->r12
=
146 regs
->r13
= regs
->r14
= regs
->r15
= 0;
148 current
->mm
->end_code
= ex
.a_text
+
149 (current
->mm
->start_code
= N_TXTADDR(ex
));
150 current
->mm
->end_data
= ex
.a_data
+
151 (current
->mm
->start_data
= N_DATADDR(ex
));
152 current
->mm
->brk
= ex
.a_bss
+
153 (current
->mm
->start_brk
= N_BSSADDR(ex
));
155 retval
= setup_arg_pages(bprm
, IA32_STACK_TOP
, EXSTACK_DEFAULT
);
159 install_exec_creds(bprm
);
161 if (N_MAGIC(ex
) == OMAGIC
) {
162 unsigned long text_addr
, map_size
;
164 text_addr
= N_TXTADDR(ex
);
165 map_size
= ex
.a_text
+ex
.a_data
;
167 error
= vm_brk(text_addr
& PAGE_MASK
, map_size
);
172 error
= read_code(bprm
->file
, text_addr
, 32,
173 ex
.a_text
+ ex
.a_data
);
174 if ((signed long)error
< 0)
178 static unsigned long error_time
, error_time2
;
179 if ((ex
.a_text
& 0xfff || ex
.a_data
& 0xfff) &&
180 (N_MAGIC(ex
) != NMAGIC
) &&
181 time_after(jiffies
, error_time2
+ 5*HZ
)) {
182 printk(KERN_NOTICE
"executable not page aligned\n");
183 error_time2
= jiffies
;
186 if ((fd_offset
& ~PAGE_MASK
) != 0 &&
187 time_after(jiffies
, error_time
+ 5*HZ
)) {
189 "fd_offset is not page aligned. Please convert "
192 error_time
= jiffies
;
196 if (!bprm
->file
->f_op
->mmap
|| (fd_offset
& ~PAGE_MASK
) != 0) {
197 error
= vm_brk(N_TXTADDR(ex
), ex
.a_text
+ex
.a_data
);
201 read_code(bprm
->file
, N_TXTADDR(ex
), fd_offset
,
202 ex
.a_text
+ex
.a_data
);
206 error
= vm_mmap(bprm
->file
, N_TXTADDR(ex
), ex
.a_text
,
207 PROT_READ
| PROT_EXEC
,
208 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
|
209 MAP_EXECUTABLE
| MAP_32BIT
,
212 if (error
!= N_TXTADDR(ex
))
215 error
= vm_mmap(bprm
->file
, N_DATADDR(ex
), ex
.a_data
,
216 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
217 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
|
218 MAP_EXECUTABLE
| MAP_32BIT
,
219 fd_offset
+ ex
.a_text
);
220 if (error
!= N_DATADDR(ex
))
225 error
= set_brk(current
->mm
->start_brk
, current
->mm
->brk
);
229 set_binfmt(&aout_format
);
231 current
->mm
->start_stack
=
232 (unsigned long)create_aout_tables((char __user
*)bprm
->p
, bprm
);
235 loadsegment(ds
, __USER32_DS
);
236 loadsegment(es
, __USER32_DS
);
238 (regs
)->ip
= ex
.a_entry
;
239 (regs
)->sp
= current
->mm
->start_stack
;
240 (regs
)->flags
= 0x200;
241 (regs
)->cs
= __USER32_CS
;
242 (regs
)->ss
= __USER32_DS
;
243 regs
->r8
= regs
->r9
= regs
->r10
= regs
->r11
=
244 regs
->r12
= regs
->r13
= regs
->r14
= regs
->r15
= 0;
249 static int load_aout_library(struct file
*file
)
251 unsigned long bss
, start_addr
, len
, error
;
257 error
= kernel_read(file
, &ex
, sizeof(ex
), &pos
);
258 if (error
!= sizeof(ex
))
261 /* We come in here for the regular a.out style of shared libraries */
262 if ((N_MAGIC(ex
) != ZMAGIC
&& N_MAGIC(ex
) != QMAGIC
) || N_TRSIZE(ex
) ||
263 N_DRSIZE(ex
) || ((ex
.a_entry
& 0xfff) && N_MAGIC(ex
) == ZMAGIC
) ||
264 i_size_read(file_inode(file
)) <
265 ex
.a_text
+ex
.a_data
+N_SYMSIZE(ex
)+N_TXTOFF(ex
)) {
272 /* For QMAGIC, the starting address is 0x20 into the page. We mask
273 this off to get the starting address for the page */
275 start_addr
= ex
.a_entry
& 0xfffff000;
277 if ((N_TXTOFF(ex
) & ~PAGE_MASK
) != 0) {
279 static unsigned long error_time
;
280 if (time_after(jiffies
, error_time
+ 5*HZ
)) {
282 "N_TXTOFF is not page aligned. Please convert "
285 error_time
= jiffies
;
288 retval
= vm_brk(start_addr
, ex
.a_text
+ ex
.a_data
+ ex
.a_bss
);
292 read_code(file
, start_addr
, N_TXTOFF(ex
),
293 ex
.a_text
+ ex
.a_data
);
297 /* Now use mmap to map the library into memory. */
298 error
= vm_mmap(file
, start_addr
, ex
.a_text
+ ex
.a_data
,
299 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
300 MAP_FIXED
| MAP_PRIVATE
| MAP_DENYWRITE
| MAP_32BIT
,
303 if (error
!= start_addr
)
306 len
= PAGE_ALIGN(ex
.a_text
+ ex
.a_data
);
307 bss
= ex
.a_text
+ ex
.a_data
+ ex
.a_bss
;
309 retval
= vm_brk(start_addr
+ len
, bss
- len
);
318 static int __init
init_aout_binfmt(void)
320 register_binfmt(&aout_format
);
324 static void __exit
exit_aout_binfmt(void)
326 unregister_binfmt(&aout_format
);
329 module_init(init_aout_binfmt
);
330 module_exit(exit_aout_binfmt
);
331 MODULE_LICENSE("GPL");