dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / sgs / mcs / common / utils.c
blob587f175c430f52c65510da3c500f026c17a41c14
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright(c) 1988 AT&T
23 * All Rights Reserved
28 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
29 * Use is subject to license terms.
32 #include "mcs.h"
33 #include "extern.h"
34 #include "gelf.h"
37 * Function prototypes.
39 static void docompress(section_info_table *);
40 static char *compress(char *, size_t *);
41 static void doappend(char *, section_info_table *);
42 static void doprint(char *, section_info_table *);
43 static void dozap(section_info_table *);
44 static int dohash(char *);
49 * Apply the actions specified by the user.
51 int
52 apply_action(section_info_table *info, char *cur_file, Cmd_Info *cmd_info)
54 int act_index;
55 int ret = 0;
56 GElf_Shdr shdr;
58 (void) gelf_getshdr(info->scn, &shdr);
59 for (act_index = 0; act_index < actmax; act_index++) {
60 Action[act_index].a_cnt++;
61 switch (Action[act_index].a_action) {
62 case ACT_ZAP:
63 if (GET_ACTION(info->flags) == ACT_DELETE)
64 break;
65 dozap(info);
66 SET_ACTION(info->flags, ACT_ZAP);
67 SET_MODIFIED(info->flags);
68 break;
69 case ACT_PRINT:
70 if (GET_ACTION(info->flags) == ACT_DELETE)
71 break;
72 if (shdr.sh_type == SHT_NOBITS) {
73 error_message(ACT_PRINT_ERROR, PLAIN_ERROR,
74 NULL, prog, cur_file, SECT_NAME);
75 break;
77 doprint(cur_file, info);
78 break;
79 case ACT_DELETE:
81 * If I am strip command, this is the
82 * only action I can take.
84 if (GET_ACTION(info->flags) == ACT_DELETE)
85 break;
86 if (GET_LOC(info->flags) == IN) {
88 * If I am 'strip', I have to
89 * unset the candidate flag and
90 * unset the error return code.
92 if (CHK_OPT(info, I_AM_STRIP)) {
93 ret = 0;
94 UNSET_CANDIDATE(info->flags);
95 } else {
96 char *name = info->name;
98 ret++;
99 if (name == NULL)
100 name = gettext("<unknown>");
101 error_message(ACT_DELETE1_ERROR,
102 PLAIN_ERROR, NULL,
103 prog, cur_file, name);
105 break;
106 } else if (info->rel_loc == IN) {
108 * If I am 'strip', I have to
109 * unset the candidate flag and
110 * unset the error return code.
112 if (CHK_OPT(info, I_AM_STRIP)) {
113 ret = 0;
114 UNSET_CANDIDATE(info->flags);
115 } else {
116 ret++;
117 error_message(ACT_DELETE2_ERROR,
118 PLAIN_ERROR, NULL,
119 prog, cur_file, SECT_NAME,
120 info->rel_name);
122 break;
123 } else if (GET_LOC(info->flags) == PRIOR) {
125 * I can not delete this
126 * section. I can only NULL
127 * this out.
129 info->secno = (GElf_Word)NULLED;
130 (cmd_info->no_of_nulled)++;
131 } else {
132 info->secno = (GElf_Word)DELETED;
133 (cmd_info->no_of_delete)++;
135 SET_ACTION(info->flags, ACT_DELETE);
136 SET_MODIFIED(info->flags);
137 break;
138 case ACT_APPEND:
139 if (shdr.sh_type == SHT_NOBITS) {
140 ret++;
141 error_message(ACT_APPEND1_ERROR, PLAIN_ERROR,
142 NULL, prog, cur_file, SECT_NAME);
143 break;
144 } else if (GET_LOC(info->flags) == IN) {
145 ret++;
146 error_message(ACT_APPEND2_ERROR, PLAIN_ERROR,
147 NULL, prog, cur_file, SECT_NAME);
148 break;
150 doappend(Action[act_index].a_string, info);
151 (cmd_info->no_of_append)++;
152 info->secno = info->osecno;
153 SET_ACTION(info->flags, ACT_APPEND);
154 SET_MODIFIED(info->flags);
155 if (GET_LOC(info->flags) == PRIOR)
156 info->secno = (GElf_Word)EXPANDED;
157 break;
158 case ACT_COMPRESS:
160 * If this section is already deleted,
161 * don't do anything.
163 if (GET_ACTION(info->flags) == ACT_DELETE)
164 break;
165 if (shdr.sh_type == SHT_NOBITS) {
166 ret++;
167 error_message(ACT_COMPRESS1_ERROR, PLAIN_ERROR,
168 NULL, prog, cur_file, SECT_NAME);
169 break;
170 } else if (GET_LOC(info->flags) == IN) {
171 ret++;
172 error_message(ACT_COMPRESS2_ERROR, PLAIN_ERROR,
173 NULL, prog, cur_file, SECT_NAME);
174 break;
177 docompress(info);
178 (cmd_info->no_of_compressed)++;
179 SET_ACTION(info->flags, ACT_COMPRESS);
180 SET_MODIFIED(info->flags);
181 if (GET_LOC(info->flags) == PRIOR)
182 info->secno = (GElf_Word)SHRUNK;
183 break;
186 return (ret);
190 * ACT_ZAP
192 static void
193 dozap(section_info_table *info)
195 Elf_Data *data;
197 info->mdata = data = malloc(sizeof (Elf_Data));
198 if (data == NULL) {
199 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL, prog);
200 mcs_exit(FAILURE);
202 *data = *info->data;
203 data->d_buf = calloc(1, data->d_size);
204 if (data->d_buf == NULL) {
205 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL, prog);
206 mcs_exit(FAILURE);
211 * ACT_PRINT
213 static void
214 doprint(char *cur_file, section_info_table *info)
216 Elf_Data *data;
217 size_t temp_size;
218 char *temp_string;
220 if (GET_MODIFIED(info->flags) == 0)
221 data = info->data;
222 else
223 data = info->mdata;
224 if (data == 0)
225 return;
227 temp_size = data->d_size;
228 temp_string = data->d_buf;
230 if (temp_size == 0)
231 return;
232 (void) fprintf(stdout, "%s:\n", cur_file);
234 while (temp_size--) {
235 char c = *temp_string++;
236 switch (c) {
237 case '\0':
238 (void) putchar('\n');
239 break;
240 default:
241 (void) putchar(c);
242 break;
245 (void) putchar('\n');
249 * ACT_APPEND
251 static void
252 doappend(char *a_string, section_info_table *info)
254 Elf_Data *data;
255 char *p;
256 size_t len;
257 char *tp;
260 * Get the length of the string to be added. We accept any
261 * string (even null), as this is arbitrary user defined text.
263 * The caller expects this routine to replace a NULL info->mdata
264 * field with a pointer to a freshly allocated copy. Any attempt
265 * to optimize away a null string append would have to deal with
266 * that, as failing to do so will cause a segfault when the NULL
267 * mdata field is dereferenced. Accepting null strings in
268 * this very unimportant case eliminates the need for that.
270 len = strlen(a_string);
273 * Every modification operation will be done
274 * to a new Elf_Data descriptor.
276 if (info->mdata == 0) {
278 * mdata is not allocated yet.
279 * Allocate the data and set it.
281 info->mdata = data = calloc(1, sizeof (Elf_Data));
282 if (data == NULL) {
283 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL, prog);
284 mcs_exit(FAILURE);
286 *data = *info->data;
289 * Check if the section is deleted or not.
290 * Or if the size is 0 or not.
292 if ((GET_ACTION(info->flags) == ACT_DELETE) ||
293 data->d_size == 0) {
295 * The section was deleated.
296 * But now, the user wants to add data to this
297 * section.
299 data->d_buf = calloc(1, len + 2);
300 if (data->d_buf == NULL) {
301 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL,
302 prog);
303 mcs_exit(FAILURE);
305 tp = (char *)data->d_buf;
306 (void) memcpy(& tp[1], a_string, len + 1);
307 data->d_size = len + 2;
308 } else {
310 * The user wants to add data to the section.
311 * I am not going to change the original data.
312 * Do the modification on the new one.
314 p = malloc(len + 1 + data->d_size);
315 if (p == NULL) {
316 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL,
317 prog);
318 mcs_exit(FAILURE);
320 (void) memcpy(p, data->d_buf, data->d_size);
321 (void) memcpy(&p[data->d_size], a_string, len + 1);
322 data->d_buf = p;
323 data->d_size = data->d_size + len + 1;
325 } else {
327 * mdata is already allocated.
328 * Modify it.
330 data = info->mdata;
331 if ((GET_ACTION(info->flags) == ACT_DELETE) ||
332 data->d_size == 0) {
334 * The section was deleated.
335 * But now, the user wants to add data to this
336 * section.
338 free(data->d_buf);
339 data->d_buf = calloc(1, len + 2);
340 if (data->d_buf == NULL) {
341 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL,
342 prog);
343 mcs_exit(FAILURE);
345 tp = (char *)data->d_buf;
346 (void) memcpy(&tp[1], a_string, len + 1);
347 data->d_size = len + 2;
348 } else {
350 * The user wants to add data to the section.
351 * I am not going to change the original data.
352 * Do the modification on the new one.
354 p = malloc(len + 1 + data->d_size);
355 if (p == NULL) {
356 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL,
357 prog);
358 mcs_exit(FAILURE);
360 (void) memcpy(p, data->d_buf, data->d_size);
361 (void) memcpy(&p[data->d_size], a_string, len + 1);
362 free(data->d_buf);
363 data->d_buf = p;
364 data->d_size = data->d_size + len + 1;
370 * ACT_COMPRESS
372 #define HALFLONG 16
373 #define low(x) (x&((1L<<HALFLONG)-1))
374 #define high(x) (x>>HALFLONG)
376 static void
377 docompress(section_info_table *info)
379 Elf_Data *data;
380 size_t size;
381 char *buf;
383 if (info->mdata == 0) {
385 * mdata is not allocated yet.
386 * Allocate the data and set it.
388 char *p;
389 info->mdata = data = calloc(1, sizeof (Elf_Data));
390 if (data == NULL) {
391 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL, prog);
392 mcs_exit(FAILURE);
394 *data = *info->data;
395 p = malloc(data->d_size);
396 (void) memcpy(p, (char *)data->d_buf, data->d_size);
397 data->d_buf = p;
399 size = info->mdata->d_size;
400 buf = (char *)info->mdata->d_buf;
401 buf = compress(buf, &size);
402 info->mdata->d_buf = buf;
403 info->mdata->d_size = size;
406 static char *
407 compress(char *str, size_t *size)
409 int hash;
410 int i;
411 size_t temp_string_size = 0;
412 size_t o_size = *size;
413 char *temp_string = str;
415 int *hash_key;
416 size_t hash_num;
417 size_t hash_end;
418 size_t *hash_str;
419 char *strings;
420 size_t next_str;
421 size_t str_size;
423 hash_key = malloc(sizeof (int) * 200);
424 hash_end = 200;
425 hash_str = malloc(sizeof (size_t) * 200);
426 str_size = o_size+1;
427 strings = malloc(str_size);
429 if (hash_key == NULL || hash_str == NULL || strings == NULL) {
430 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL, prog);
431 mcs_exit(FAILURE);
434 hash_num = 0;
435 next_str = 0;
437 while (temp_string_size < o_size) {
438 size_t pos;
439 char c;
441 * Get a string
443 pos = next_str;
445 while ((c = *(temp_string++)) != '\0' &&
446 (temp_string_size + (next_str - pos)) <= o_size) {
447 if (next_str >= str_size) {
448 str_size *= 2;
449 if ((strings = (char *)
450 realloc(strings, str_size)) == NULL) {
451 error_message(MALLOC_ERROR, PLAIN_ERROR,
452 NULL, prog);
453 mcs_exit(FAILURE);
456 strings[next_str++] = c;
459 if (next_str >= str_size) {
460 str_size *= 2;
461 if ((strings = (char *)
462 realloc(strings, str_size)) == NULL) {
463 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL,
464 prog);
465 mcs_exit(FAILURE);
468 strings[next_str++] = '\0';
470 * End get string
473 temp_string_size += (next_str - pos);
474 hash = dohash(pos + strings);
475 for (i = 0; i < hash_num; i++) {
476 if (hash != hash_key[i])
477 continue;
478 if (strcmp(pos + strings, hash_str[i] + strings) == 0)
479 break;
481 if (i != hash_num) {
482 next_str = pos;
483 continue;
485 if (hash_num == hash_end) {
486 hash_end *= 2;
487 hash_key = reallocarray(hash_key, hash_end,
488 sizeof (int));
489 hash_str = reallocarray(hash_str, hash_end,
490 sizeof (size_t));
491 if (hash_key == NULL || hash_str == NULL) {
492 error_message(MALLOC_ERROR, PLAIN_ERROR, NULL,
493 prog);
494 mcs_exit(FAILURE);
497 hash_key[hash_num] = hash;
498 hash_str[hash_num++] = pos;
502 * Clean up
504 free(hash_key);
505 free(hash_str);
508 * Return
510 if (next_str != o_size) {
512 * string compressed.
514 *size = next_str;
515 free(str);
516 str = malloc(next_str);
517 (void) memcpy(str, strings, next_str);
519 free(strings);
520 return (str);
523 static int
524 dohash(char *str)
526 long sum;
527 unsigned shift;
528 int t;
529 sum = 1;
530 for (shift = 0; (t = *str++) != 0; shift += 7) {
531 sum += (long)t << (shift %= HALFLONG);
533 sum = low(sum) + high(sum);
534 /* LINTED */
535 return ((short)low(sum) + (short)high(sum));