4 Copyright 2008-11 Michel Pollet <buserror@gmail.com>
6 This program cross examines a root filesystem, loads all the elf
7 files it can find, see what other library they load and then
8 find the orphans. In then remove the orphans as "user" for it's
9 dependencies and continues removing until everything has at least
10 one user, OR is a program itself (ie, not a shared library)
12 cross_linker is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 cross_linker is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with cross_linker. If not, see <http://www.gnu.org/licenses/>.
31 /********************************************************************
33 ********************************************************************/
35 static uint8_t _crc16_lh[16] = { 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60,
36 0x70, 0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1 };
37 static uint8_t _crc16_ll[16] = { 0x00, 0x21, 0x42, 0x63, 0x84, 0xA5, 0xC6,
38 0xE7, 0x08, 0x29, 0x4A, 0x6B, 0x8C, 0xAD, 0xCE, 0xEF };
40 static uint16_t crc16_u4(uint16_t crc, uint8_t val)
42 uint8_t h = crc >> 8, l = crc & 0xff;
43 uint8_t t = (h >> 4) ^ val;
45 // Shift the CRC Register left 4 bits
46 h = (h << 4) | (l >> 4);
48 // Do the table lookups and XOR the result into the CRC Tables
54 static uint16_t crc16_update(uint16_t crc, uint8_t val)
56 crc = crc16_u4(crc, val >> 4); // High nibble first
57 crc = crc16_u4(crc, val & 0x0F); // Low nibble
61 static uint16_t crc16_string(char * str)
63 uint16_t crc = 0xffff;
65 crc = crc16_update(crc, *str++);
69 /********************************************************************
71 ********************************************************************/
74 typedef struct str_t {
75 uint32_t hash : 16, rom : 1, len : 15;
79 static inline str_p str_new_i(const char *s, void * (*_alloc)(size_t))
81 int l = s ? strlen(s) : 0;
82 str_p r = (str_p)_alloc(sizeof(*r) + l + 1);
83 r->hash = 0; r->len = l;
88 static inline void str_free(str_p s)
93 static inline str_p str_new(const char *s)
95 return str_new_i(s, malloc);
97 static inline str_p str_anew(const char *s)
99 str_p r = str_new_i(s, alloca);
103 static inline str_p str_dup(const str_p s)
105 size_t l = sizeof(*s) + s->len + 1;
106 str_p r = (str_p)malloc(l);
110 static inline str_p str_adup(const str_p s)
112 size_t l = sizeof(*s) + s->len + 1;
113 str_p r = (str_p)alloca(l);
118 static inline uint16_t str_hash(str_p s)
120 if (!s->hash) s->hash = crc16_string(s->str);
123 static inline int str_cmp(str_p s1, str_p s2)
125 if (s1 == s2) return 1;
126 if (s1->len != s2->len) return 1;
129 return s1->hash == s2->hash ? strcmp(s1->str, s2->str) : 1;
132 /********************************************************************
133 * Hash table of strings. Key/value pair
134 ********************************************************************/
136 typedef struct str_hashval_t {
141 DECLARE_C_ARRAY(str_hashval_t, hashval_array, 16);
142 IMPLEMENT_C_ARRAY(hashval_array);
144 #ifndef STR_HASH_SIZE
145 #define STR_HASH_SIZE 512 // use 9 bits of the 16 of the CRC
147 /* uses bins to store the strings as per their hash values */
148 typedef struct str_hash_t {
149 hashval_array_t bin[STR_HASH_SIZE];
150 } str_hash_t, *str_hash_p;
166 #endif /* __C_UTILS_H__ */