Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 2016 Denys Vlasenko |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 8 | */ |
| 9 | //config:config FEATURE_USE_BSS_TAIL |
| 10 | //config: bool "Use the end of BSS page" |
| 11 | //config: default n |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: Attempt to reclaim a small unused part of BSS. |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 14 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: Executables have the following parts: |
| 16 | //config: = read-only executable code and constants, also known as "text" |
| 17 | //config: = read-write data |
| 18 | //config: = non-initialized (zeroed on demand) data, also known as "bss" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 19 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 20 | //config: At link time, "text" is padded to a full page. At runtime, all "text" |
| 21 | //config: pages are mapped RO and executable. |
Denys Vlasenko | a3df2fa | 2017-07-15 20:49:32 +0200 | [diff] [blame] | 22 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 23 | //config: "Data" starts on the next page boundary, but is not padded |
| 24 | //config: to a full page at the end. "Bss" starts wherever "data" ends. |
| 25 | //config: At runtime, "data" pages are mapped RW and they are file-backed |
| 26 | //config: (this includes a small portion of "bss" which may live in the last |
| 27 | //config: partial page of "data"). |
| 28 | //config: Pages which are fully in "bss" are mapped to anonymous memory. |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 29 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 30 | //config: "Bss" end is usually not page-aligned. There is an unused space |
| 31 | //config: in the last page. Linker marks its start with the "_end" symbol. |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 32 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 33 | //config: This option will attempt to use that space for bb_common_bufsiz1[] |
| 34 | //config: array. If it fits after _end, it will be used, and COMMON_BUFSIZE |
| 35 | //config: will be enlarged from its guaranteed minimum size of 1 kbyte. |
| 36 | //config: This may require recompilation a second time, since value of _end |
| 37 | //config: is known only after final link. |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 38 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 39 | //config: If you are getting a build error like this: |
| 40 | //config: appletlib.c:(.text.main+0xd): undefined reference to '_end' |
| 41 | //config: disable this option. |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 42 | |
| 43 | //kbuild:lib-y += common_bufsiz.o |
| 44 | |
| 45 | #include "libbb.h" |
| 46 | #include "common_bufsiz.h" |
| 47 | |
| 48 | #if !ENABLE_FEATURE_USE_BSS_TAIL |
| 49 | |
| 50 | /* We use it for "global" data via *(struct global*)bb_common_bufsiz1. |
| 51 | * Since gcc insists on aligning struct global's members, it would be a pity |
| 52 | * (and an alignment fault on some CPUs) to mess it up. */ |
| 53 | char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long)); |
| 54 | |
| 55 | #else |
| 56 | |
| 57 | # ifndef setup_common_bufsiz |
Denys Vlasenko | 834aba3 | 2016-11-04 14:13:58 +0100 | [diff] [blame] | 58 | /* For now, this is never used: |
| 59 | * scripts/generate_BUFSIZ.sh never generates "malloced" bufsiz1: |
| 60 | * enum { COMMON_BUFSIZE = 1024 }; |
| 61 | * extern char *const bb_common_bufsiz1; |
| 62 | * void setup_common_bufsiz(void); |
| 63 | * This has proved to be worse than the approach of defining |
| 64 | * larger bb_common_bufsiz1[] array. |
| 65 | */ |
| 66 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 67 | /* |
Denys Vlasenko | f56fb5e | 2016-04-21 21:03:51 +0200 | [diff] [blame] | 68 | * It is not defined as a dummy macro. |
| 69 | * It means we have to provide this function. |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 70 | */ |
Denys Vlasenko | 93e1aaa | 2016-04-21 21:47:45 +0200 | [diff] [blame] | 71 | char *const bb_common_bufsiz1 __attribute__ ((section (".data"))); |
Denys Vlasenko | f56fb5e | 2016-04-21 21:03:51 +0200 | [diff] [blame] | 72 | void setup_common_bufsiz(void) |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 73 | { |
| 74 | if (!bb_common_bufsiz1) |
Denys Vlasenko | 93e1aaa | 2016-04-21 21:47:45 +0200 | [diff] [blame] | 75 | *(char**)&bb_common_bufsiz1 = xzalloc(COMMON_BUFSIZE); |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 76 | } |
| 77 | # else |
| 78 | # ifndef bb_common_bufsiz1 |
| 79 | /* bb_common_bufsiz1[] is not aliased to _end[] */ |
| 80 | char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long)); |
| 81 | # endif |
| 82 | # endif |
| 83 | |
| 84 | #endif |