diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-11-29 03:31:20 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-11-29 03:31:20 +0000 |
commit | 745cd17926a9d75cdd6482d5ce58227b492d1ebe (patch) | |
tree | 568f3236704000eed61d3460f9eef92e9535b746 | |
parent | c253778de92441b61806e1e4e7806e91f17ea50f (diff) | |
download | busybox-w32-745cd17926a9d75cdd6482d5ce58227b492d1ebe.tar.gz busybox-w32-745cd17926a9d75cdd6482d5ce58227b492d1ebe.tar.bz2 busybox-w32-745cd17926a9d75cdd6482d5ce58227b492d1ebe.zip |
Further optimize applet tables; prettify build output
text data bss dec hex filename
775923 929 9100 785952 bfe20 busybox_old
775565 929 9100 785594 bfcba busybox_unstripped
-rw-r--r-- | applets/Kbuild | 18 | ||||
-rw-r--r-- | applets/applet_tables.c | 55 | ||||
-rw-r--r-- | include/busybox.h | 29 | ||||
-rw-r--r-- | libbb/appletlib.c | 14 | ||||
-rw-r--r-- | libbb/vfork_daemon_rexec.c | 2 |
5 files changed, 71 insertions, 47 deletions
diff --git a/applets/Kbuild b/applets/Kbuild index b9dba03e1..ef4b76fed 100644 --- a/applets/Kbuild +++ b/applets/Kbuild | |||
@@ -14,20 +14,20 @@ always:= $(hostprogs-y) | |||
14 | 14 | ||
15 | # Generated files need additional love | 15 | # Generated files need additional love |
16 | 16 | ||
17 | quiet_cmd_gen_usage_compressed = GEN include/usage_compressed.h | ||
18 | cmd_gen_usage_compressed = $(srctree)/applets/usage_compressed include/usage_compressed.h applets | ||
19 | |||
20 | HOSTCFLAGS_usage.o = -I$(srctree)/include | 17 | HOSTCFLAGS_usage.o = -I$(srctree)/include |
21 | 18 | ||
22 | applets/applets.o: include/usage_compressed.h include/applet_tables.h | 19 | applets/applets.o: include/usage_compressed.h include/applet_tables.h |
23 | 20 | ||
24 | applets/usage: .config $(srctree)/applets/usage_compressed | 21 | applets/usage: .config $(srctree)/applets/usage_compressed |
22 | |||
23 | quiet_cmd_gen_usage_compressed = GEN include/usage_compressed.h | ||
24 | cmd_gen_usage_compressed = $(srctree)/applets/usage_compressed include/usage_compressed.h applets | ||
25 | 25 | ||
26 | include/usage_compressed.h: applets/usage $(srctree)/applets/usage_compressed | 26 | include/usage_compressed.h: applets/usage $(srctree)/applets/usage_compressed |
27 | $(call cmd,gen_usage_compressed) | 27 | $(call cmd,gen_usage_compressed) |
28 | 28 | ||
29 | # Two-stage file creation, to avoid having target file still created | 29 | quiet_cmd_gen_applet_tables = GEN include/applet_tables.h |
30 | # in case applet_tables fails | 30 | cmd_gen_applet_tables = applets/applet_tables include/applet_tables.h |
31 | |||
31 | include/applet_tables.h: applets/applet_tables | 32 | include/applet_tables.h: applets/applet_tables |
32 | applets/applet_tables >include/applet_tables.h.tmp | 33 | $(call cmd,gen_applet_tables) |
33 | mv include/applet_tables.h.tmp include/applet_tables.h | ||
diff --git a/applets/applet_tables.c b/applets/applet_tables.c index b5df74076..9f42ebf31 100644 --- a/applets/applet_tables.c +++ b/applets/applet_tables.c | |||
@@ -50,45 +50,66 @@ int main(int argc, char **argv) | |||
50 | 50 | ||
51 | qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name); | 51 | qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name); |
52 | 52 | ||
53 | /* Keep in sync with include/busybox.h! */ | ||
54 | |||
55 | puts("const char applet_names[] ALIGN1 ="); | ||
56 | ofs = 0; | 53 | ofs = 0; |
57 | i = 0; | ||
58 | for (i = 0; i < NUM_APPLETS; i++) { | 54 | for (i = 0; i < NUM_APPLETS; i++) { |
59 | offset[i] = ofs; | 55 | offset[i] = ofs; |
60 | ofs += strlen(applets[i].name) + 1; | 56 | ofs += strlen(applets[i].name) + 1; |
57 | } | ||
58 | /* We reuse 4 high-order bits of offset array for other purposes, | ||
59 | * so if they are indeed needed, refuse to proceed */ | ||
60 | if (ofs > 0xfff) | ||
61 | return 1; | ||
62 | if (!argv[1]) | ||
63 | return 1; | ||
64 | |||
65 | i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666); | ||
66 | if (i < 0) | ||
67 | return 1; | ||
68 | dup2(i, 1); | ||
69 | |||
70 | /* Keep in sync with include/busybox.h! */ | ||
71 | |||
72 | puts("/* This is a generated file, don't edit */"); | ||
73 | |||
74 | puts("const char applet_names[] ALIGN1 ="); | ||
75 | for (i = 0; i < NUM_APPLETS; i++) { | ||
61 | printf("\"%s\" \"\\0\"\n", applets[i].name); | 76 | printf("\"%s\" \"\\0\"\n", applets[i].name); |
62 | } | 77 | } |
63 | puts(";"); | 78 | puts(";"); |
64 | 79 | ||
65 | puts("int (*const applet_mains[])(int argc, char **argv) = {"); | 80 | puts("int (*const applet_main[])(int argc, char **argv) = {"); |
66 | for (i = 0; i < NUM_APPLETS; i++) { | 81 | for (i = 0; i < NUM_APPLETS; i++) { |
67 | printf("%s_main,\n", applets[i].main); | 82 | printf("%s_main,\n", applets[i].main); |
68 | } | 83 | } |
69 | puts("};"); | 84 | puts("};"); |
70 | 85 | ||
71 | #if ENABLE_FEATURE_INSTALLER || ENABLE_FEATURE_PREFER_APPLETS | ||
72 | puts("const uint32_t applet_nameofs[] = {"); | ||
73 | #else | ||
74 | puts("const uint16_t applet_nameofs[] ALIGN2 = {"); | 86 | puts("const uint16_t applet_nameofs[] ALIGN2 = {"); |
75 | #endif | ||
76 | for (i = 0; i < NUM_APPLETS; i++) { | 87 | for (i = 0; i < NUM_APPLETS; i++) { |
77 | printf("0x%08x,\n", | 88 | printf("0x%04x,\n", |
78 | offset[i] | 89 | offset[i] |
90 | #if ENABLE_FEATURE_PREFER_APPLETS | ||
91 | + (applets[i].nofork << 12) | ||
92 | + (applets[i].noexec << 13) | ||
93 | #endif | ||
79 | #if ENABLE_FEATURE_SUID | 94 | #if ENABLE_FEATURE_SUID |
80 | + (applets[i].need_suid << 14) /* 2 bits */ | 95 | + (applets[i].need_suid << 14) /* 2 bits */ |
81 | #endif | 96 | #endif |
82 | #if ENABLE_FEATURE_INSTALLER | ||
83 | + (applets[i].install_loc << 16) /* 3 bits */ | ||
84 | #endif | ||
85 | #if ENABLE_FEATURE_PREFER_APPLETS | ||
86 | + (applets[i].nofork << 19) | ||
87 | + (applets[i].noexec << 20) | ||
88 | #endif | ||
89 | ); | 97 | ); |
90 | } | 98 | } |
91 | puts("};"); | 99 | puts("};"); |
92 | 100 | ||
101 | #if ENABLE_FEATURE_INSTALLER | ||
102 | puts("const uint8_t applet_install_loc[] ALIGN1 = {"); | ||
103 | i = 0; | ||
104 | while (i < NUM_APPLETS) { | ||
105 | int v = applets[i].install_loc; /* 3 bits */ | ||
106 | if (++i < NUM_APPLETS) | ||
107 | v |= applets[i].install_loc << 4; /* 3 bits */ | ||
108 | printf("0x%02x,\n", v); | ||
109 | i++; | ||
110 | } | ||
111 | puts("};"); | ||
112 | #endif | ||
113 | |||
93 | return 0; | 114 | return 0; |
94 | } | 115 | } |
diff --git a/include/busybox.h b/include/busybox.h index f99901aa3..ec6953e87 100644 --- a/include/busybox.h +++ b/include/busybox.h | |||
@@ -28,28 +28,31 @@ typedef enum bb_suid_t { | |||
28 | /* Defined in appletlib.c (by including generated applet_tables.h) */ | 28 | /* Defined in appletlib.c (by including generated applet_tables.h) */ |
29 | /* Keep in sync with applets/applet_tables.c! */ | 29 | /* Keep in sync with applets/applet_tables.c! */ |
30 | extern const char applet_names[]; | 30 | extern const char applet_names[]; |
31 | extern int (*const applet_mains[])(int argc, char **argv); | 31 | extern int (*const applet_main[])(int argc, char **argv); |
32 | extern const uint16_t applet_nameofs[]; | ||
33 | extern const uint8_t applet_install_loc[]; | ||
32 | 34 | ||
33 | #if ENABLE_FEATURE_INSTALLER || ENABLE_FEATURE_PREFER_APPLETS | 35 | #if ENABLE_FEATURE_SUID || ENABLE_FEATURE_PREFER_APPLETS |
34 | extern const uint32_t applet_nameofs[]; | 36 | #define APPLET_NAME(i) (applet_names + (applet_nameofs[i] & 0x0fff)) |
35 | #else | 37 | #else |
36 | extern const uint16_t applet_nameofs[]; | 38 | #define APPLET_NAME(i) (applet_names + applet_nameofs[i]) |
39 | #endif | ||
40 | |||
41 | #if ENABLE_FEATURE_PREFER_APPLETS | ||
42 | #define APPLET_IS_NOFORK(i) (applet_nameofs[i] & (1 << 12)) | ||
43 | #define APPLET_IS_NOEXEC(i) (applet_nameofs[i] & (1 << 13)) | ||
37 | #endif | 44 | #endif |
38 | 45 | ||
39 | #if ENABLE_FEATURE_SUID | 46 | #if ENABLE_FEATURE_SUID |
40 | #define APPLET_NAME(i) (applet_names + (applet_nameofs[i] & 0x3fff)) | ||
41 | #define APPLET_SUID(i) ((applet_nameofs[i] >> 14) & 0x3) | 47 | #define APPLET_SUID(i) ((applet_nameofs[i] >> 14) & 0x3) |
42 | #else | ||
43 | #define APPLET_NAME(i) (applet_names + (applet_nameofs[i] & 0xffff)) | ||
44 | #endif | 48 | #endif |
45 | 49 | ||
46 | #if ENABLE_FEATURE_INSTALLER | 50 | #if ENABLE_FEATURE_INSTALLER |
47 | #define APPLET_INSTALL_LOC(i) ((applet_nameofs[i] >> 16) & 0x7) | 51 | #define APPLET_INSTALL_LOC(i) ({ \ |
48 | #endif | 52 | unsigned v = (i); \ |
49 | 53 | if (v & 1) v = applet_install_loc[v/2] >> 4; \ | |
50 | #if ENABLE_FEATURE_PREFER_APPLETS | 54 | else v = applet_install_loc[v/2] & 0xf; \ |
51 | #define APPLET_IS_NOFORK(i) (applet_nameofs[i] & (1 << 19)) | 55 | v; }) |
52 | #define APPLET_IS_NOEXEC(i) (applet_nameofs[i] & (1 << 20)) | ||
53 | #endif | 56 | #endif |
54 | 57 | ||
55 | 58 | ||
diff --git a/libbb/appletlib.c b/libbb/appletlib.c index dcf24f5a9..d5a2d06af 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c | |||
@@ -101,19 +101,17 @@ void bb_show_usage(void) | |||
101 | } | 101 | } |
102 | 102 | ||
103 | 103 | ||
104 | /* NB: any char pointer will work as well, not necessarily applet_names */ | ||
104 | static int applet_name_compare(const void *name, const void *v) | 105 | static int applet_name_compare(const void *name, const void *v) |
105 | { | 106 | { |
106 | int i = (const char *)v - applet_names; | 107 | int i = (const char *)v - applet_names; |
107 | return strcmp(name, APPLET_NAME(i)); | 108 | return strcmp(name, APPLET_NAME(i)); |
108 | } | 109 | } |
109 | |||
110 | int find_applet_by_name(const char *name) | 110 | int find_applet_by_name(const char *name) |
111 | { | 111 | { |
112 | const char *p; | ||
113 | /* Do a binary search to find the applet entry given the name. */ | 112 | /* Do a binary search to find the applet entry given the name. */ |
114 | 113 | const char *p; | |
115 | /* NB: any char pointer will work as well, not necessarily applet_names */ | 114 | p = bsearch(name, applet_names, ARRAY_SIZE(applet_main), 1, applet_name_compare); |
116 | p = bsearch(name, applet_names, ARRAY_SIZE(applet_mains), 1, applet_name_compare); | ||
117 | if (!p) | 115 | if (!p) |
118 | return -1; | 116 | return -1; |
119 | return p - applet_names; | 117 | return p - applet_names; |
@@ -543,10 +541,12 @@ static void install_links(const char *busybox, int use_symbolic_links) | |||
543 | if (use_symbolic_links) | 541 | if (use_symbolic_links) |
544 | lf = symlink; | 542 | lf = symlink; |
545 | 543 | ||
546 | for (i = 0; i < ARRAY_SIZE(applet_mains); i++) { | 544 | for (i = 0; i < ARRAY_SIZE(applet_main); i++) { |
547 | fpc = concat_path_file( | 545 | fpc = concat_path_file( |
548 | install_dir[APPLET_INSTALL_LOC(i)], | 546 | install_dir[APPLET_INSTALL_LOC(i)], |
549 | APPLET_NAME(i)); | 547 | APPLET_NAME(i)); |
548 | // debug: bb_error_msg("%slinking %s to busybox", | ||
549 | // use_symbolic_links ? "sym" : "", fpc); | ||
550 | rc = lf(busybox, fpc); | 550 | rc = lf(busybox, fpc); |
551 | if (rc != 0 && errno != EEXIST) { | 551 | if (rc != 0 && errno != EEXIST) { |
552 | bb_simple_perror_msg(fpc); | 552 | bb_simple_perror_msg(fpc); |
@@ -644,7 +644,7 @@ void run_applet_no_and_exit(int applet_no, char **argv) | |||
644 | bb_show_usage(); | 644 | bb_show_usage(); |
645 | if (ENABLE_FEATURE_SUID) | 645 | if (ENABLE_FEATURE_SUID) |
646 | check_suid(applet_no); | 646 | check_suid(applet_no); |
647 | exit(applet_mains[applet_no](argc, argv)); | 647 | exit(applet_main[applet_no](argc, argv)); |
648 | } | 648 | } |
649 | 649 | ||
650 | void run_applet_and_exit(const char *name, char **argv) | 650 | void run_applet_and_exit(const char *name, char **argv) |
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c index 1d6817ee6..d6e233ac3 100644 --- a/libbb/vfork_daemon_rexec.c +++ b/libbb/vfork_daemon_rexec.c | |||
@@ -143,7 +143,7 @@ int run_nofork_applet_prime(struct nofork_save_area *old, int applet_no, char ** | |||
143 | char *tmp_argv[argc+1]; | 143 | char *tmp_argv[argc+1]; |
144 | memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0])); | 144 | memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0])); |
145 | /* Finally we can call NOFORK applet's main() */ | 145 | /* Finally we can call NOFORK applet's main() */ |
146 | rc = applet_mains[applet_no](argc, tmp_argv); | 146 | rc = applet_main[applet_no](argc, tmp_argv); |
147 | } else { /* xfunc died in NOFORK applet */ | 147 | } else { /* xfunc died in NOFORK applet */ |
148 | /* in case they meant to return 0... */ | 148 | /* in case they meant to return 0... */ |
149 | if (rc == -2222) | 149 | if (rc == -2222) |