Skip to content

Commit 294602d

Browse files
committed
Convert the string tables to position-independent code.
1 parent c1e5bb4 commit 294602d

File tree

6 files changed

+103
-38
lines changed

6 files changed

+103
-38
lines changed

ChangeLog

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
2003-05-22 Bruno Haible <bruno@clisp.org>
2+
3+
* lib/genaliases.c (main): Emit %pic instead of %null-strings.
4+
Change type of 'name' field to 'int'.
5+
* lib/genaliases2.c (emit_encoding): Add a 'tag' argument. Emit an
6+
invocation of macro S(), including the tag and a counter.
7+
(main): Use the command-line argument as tag.
8+
* Makefile.devel (lib/aliases_aix.h, lib/aliases_osf1.h,
9+
lib/aliases_dos.h, lib/aliases_extra.h): Pass a tag to the program.
10+
* lib/iconv.c (stringpool2_t): New type.
11+
(stringpool2_contents): New data table.
12+
(stringpool2): New macro.
13+
(sysdep_aliases): Make position-independent. Move #includes out to
14+
aliases2.h.
15+
(aliases2_lookup): Update.
16+
(nalias): New type.
17+
(compare_by_index): Use 'struct nalias' instead of 'struct alias'.
18+
(iconvlist): Convert 'struct alias' to 'struct nalias' while copying.
19+
* lib/aliases2.h: New file, extracted from lib/iconv.c.
20+
121
2003-05-20 Bruno Haible <bruno@clisp.org>
222

323
* lib/iconv.c (iconvlist): Test p->name against NULL, not against "".

Makefile.devel

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,22 @@ lib/aliases.gperf : lib/encodings.def lib/encodings_local.def lib/genaliases.c
181181

182182
lib/aliases_aix.h : lib/encodings_aix.def lib/genaliases2.c
183183
$(CC) $(CFLAGS) -DUSE_AIX lib/genaliases2.c -o genaliases
184-
./genaliases > lib/aliases_aix.h
184+
./genaliases aix > lib/aliases_aix.h
185185
$(RM) genaliases
186186

187187
lib/aliases_osf1.h : lib/encodings_osf1.def lib/genaliases2.c
188188
$(CC) $(CFLAGS) -DUSE_OSF1 lib/genaliases2.c -o genaliases
189-
./genaliases > lib/aliases_osf1.h
189+
./genaliases osf1 > lib/aliases_osf1.h
190190
$(RM) genaliases
191191

192192
lib/aliases_dos.h : lib/encodings_dos.def lib/genaliases2.c
193193
$(CC) $(CFLAGS) -DUSE_DOS lib/genaliases2.c -o genaliases
194-
./genaliases > lib/aliases_dos.h
194+
./genaliases dos > lib/aliases_dos.h
195195
$(RM) genaliases
196196

197197
lib/aliases_extra.h : lib/encodings_extra.def lib/genaliases2.c
198198
$(CC) $(CFLAGS) -DUSE_EXTRA lib/genaliases2.c -o genaliases
199-
./genaliases > lib/aliases_extra.h
199+
./genaliases extra > lib/aliases_extra.h
200200
$(RM) genaliases
201201

202202

lib/aliases2.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 1999-2003 Free Software Foundation, Inc.
3+
* This file is part of the GNU LIBICONV Library.
4+
*
5+
* The GNU LIBICONV Library is free software; you can redistribute it
6+
* and/or modify it under the terms of the GNU Library General Public
7+
* License as published by the Free Software Foundation; either version 2
8+
* of the License, or (at your option) any later version.
9+
*
10+
* The GNU LIBICONV Library is distributed in the hope that it will be
11+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Library General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Library General Public
16+
* License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17+
* If not, write to the Free Software Foundation, Inc., 59 Temple Place -
18+
* Suite 330, Boston, MA 02111-1307, USA.
19+
*/
20+
21+
#ifdef USE_AIX
22+
#include "aliases_aix.h"
23+
#endif
24+
#ifdef USE_OSF1
25+
#include "aliases_osf1.h"
26+
#endif
27+
#ifdef USE_DOS
28+
#include "aliases_dos.h"
29+
#endif
30+
#ifdef USE_EXTRA
31+
#include "aliases_extra.h"
32+
#endif

lib/genaliases.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void emit_encoding (const char* const* names, size_t n, const char* c_nam
4040

4141
int main ()
4242
{
43-
printf("struct alias { const char* name; unsigned int encoding_index; };\n");
43+
printf("struct alias { int name; unsigned int encoding_index; };\n");
4444
printf("%%struct-type\n");
4545
printf("%%language=ANSI-C\n");
4646
printf("%%define hash-function-name aliases_hash\n");
@@ -49,7 +49,7 @@ int main ()
4949
printf("%%readonly-tables\n");
5050
printf("%%global-table\n");
5151
printf("%%define word-array-name aliases\n");
52-
printf("%%null-strings\n");
52+
printf("%%pic\n");
5353
printf("%%%%\n");
5454

5555
#define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \

lib/genaliases2.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 1999-2002 Free Software Foundation, Inc.
1+
/* Copyright (C) 1999-2003 Free Software Foundation, Inc.
22
This file is part of the GNU LIBICONV Library.
33
44
The GNU LIBICONV Library is free software; you can redistribute it
@@ -21,10 +21,11 @@
2121
#include <stdio.h>
2222
#include <stdlib.h>
2323

24-
static void emit_encoding (const char* const* names, size_t n, const char* c_name)
24+
static void emit_encoding (const char* tag, const char* const* names, size_t n, const char* c_name)
2525
{
26-
for (; n > 0; names++, n--) {
27-
printf(" { \"");
26+
static unsigned int counter = 0;
27+
for (; n > 0; names++, n--, counter++) {
28+
printf(" S(%s_%u, \"",tag,counter);
2829
/* Output *names in upper case. */
2930
{
3031
const char* s = *names;
@@ -37,16 +38,17 @@ static void emit_encoding (const char* const* names, size_t n, const char* c_nam
3738
putc(c, stdout);
3839
}
3940
}
40-
printf("\", ei_%s },\n", c_name);
41+
printf("\", ei_%s )\n", c_name);
4142
}
4243
}
4344

44-
int main ()
45+
int main (int argc, char* argv[])
4546
{
47+
const char * tag = (argc > 1 ? argv[1] : "xxx");
4648
#define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \
4749
{ \
4850
static const char* const names[] = BRACIFY xxx_names; \
49-
emit_encoding(names,sizeof(names)/sizeof(names[0]),#xxx); \
51+
emit_encoding(tag,names,sizeof(names)/sizeof(names[0]),#xxx); \
5052
}
5153
#define BRACIFY(...) { __VA_ARGS__ }
5254
#ifdef USE_AIX

lib/iconv.c

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static struct encoding const all_encodings[] = {
132132
/*
133133
* Alias lookup function.
134134
* Defines
135-
* struct alias { const char* name; unsigned int encoding_index; };
135+
* struct alias { int name; unsigned int encoding_index; };
136136
* const struct alias * aliases_lookup (const char *str, unsigned int len);
137137
* #define MAX_WORD_LENGTH ...
138138
*/
@@ -144,30 +144,32 @@ static struct encoding const all_encodings[] = {
144144
* const struct alias * aliases2_lookup (const char *str);
145145
*/
146146
#if defined(USE_AIX) || defined(USE_OSF1) || defined(USE_DOS) || defined(USE_EXTRA) /* || ... */
147-
static struct alias sysdep_aliases[] = {
148-
#ifdef USE_AIX
149-
#include "aliases_aix.h"
150-
#endif
151-
#ifdef USE_OSF1
152-
#include "aliases_osf1.h"
153-
#endif
154-
#ifdef USE_DOS
155-
#include "aliases_dos.h"
156-
#endif
157-
#ifdef USE_EXTRA
158-
#include "aliases_extra.h"
159-
#endif
147+
struct stringpool2_t {
148+
#define S(tag,name,encoding_index) char stringpool_##tag[sizeof(name)];
149+
#include "aliases2.h"
150+
#undef S
151+
};
152+
static const struct stringpool2_t stringpool2_contents = {
153+
#define S(tag,name,encoding_index) name,
154+
#include "aliases2.h"
155+
#undef S
156+
};
157+
#define stringpool2 ((const char *) &stringpool2_contents)
158+
static const struct alias sysdep_aliases[] = {
159+
#define S(tag,name,encoding_index) { (int)(long)&((struct stringpool2_t *)0)->stringpool_##tag, encoding_index },
160+
#include "aliases2.h"
161+
#undef S
160162
};
161163
#ifdef __GNUC__
162164
__inline
163165
#endif
164166
const struct alias *
165167
aliases2_lookup (register const char *str)
166168
{
167-
struct alias * ptr;
169+
const struct alias * ptr;
168170
unsigned int count;
169171
for (ptr = sysdep_aliases, count = sizeof(sysdep_aliases)/sizeof(sysdep_aliases[0]); count > 0; ptr++, count--)
170-
if (!strcmp(str,ptr->name))
172+
if (!strcmp(str, stringpool2 + ptr->name))
171173
return ptr;
172174
return NULL;
173175
}
@@ -464,10 +466,13 @@ int iconvctl (iconv_t icd, int request, void* argument)
464466
}
465467
}
466468

469+
/* An alias after its name has been converted from 'int' to 'const char*'. */
470+
struct nalias { const char* name; unsigned int encoding_index; };
471+
467472
static int compare_by_index (const void * arg1, const void * arg2)
468473
{
469-
const struct alias * alias1 = (const struct alias *) arg1;
470-
const struct alias * alias2 = (const struct alias *) arg2;
474+
const struct nalias * alias1 = (const struct nalias *) arg1;
475+
const struct nalias * alias2 = (const struct nalias *) arg2;
471476
return (int)alias1->encoding_index - (int)alias2->encoding_index;
472477
}
473478

@@ -496,7 +501,7 @@ void iconvlist (int (*do_one) (unsigned int namescount,
496501
#define aliascount2 0
497502
#endif
498503
#define aliascount (aliascount1+aliascount2)
499-
struct alias aliasbuf[aliascount];
504+
struct nalias aliasbuf[aliascount];
500505
const char * namesbuf[aliascount];
501506
size_t num_aliases;
502507
{
@@ -506,20 +511,26 @@ void iconvlist (int (*do_one) (unsigned int namescount,
506511
j = 0;
507512
for (i = 0; i < aliascount1; i++) {
508513
const struct alias * p = &aliases[i];
509-
if (p->name != NULL
514+
if (p->name >= 0
510515
&& p->encoding_index != ei_local_char
511-
&& p->encoding_index != ei_local_wchar_t)
512-
aliasbuf[j++] = *p;
516+
&& p->encoding_index != ei_local_wchar_t) {
517+
aliasbuf[j].name = stringpool + p->name;
518+
aliasbuf[j].encoding_index = p->encoding_index;
519+
j++;
520+
}
513521
}
514522
#ifndef aliases2_lookup
515-
for (i = 0; i < aliascount2; i++)
516-
aliasbuf[j++] = sysdep_aliases[i];
523+
for (i = 0; i < aliascount2; i++) {
524+
aliasbuf[j].name = stringpool2 + sysdep_aliases[i].name;
525+
aliasbuf[j].encoding_index = sysdep_aliases[i].encoding_index;
526+
j++;
527+
}
517528
#endif
518529
num_aliases = j;
519530
}
520531
/* Sort by encoding_index. */
521532
if (num_aliases > 1)
522-
qsort(aliasbuf, num_aliases, sizeof(struct alias), compare_by_index);
533+
qsort(aliasbuf, num_aliases, sizeof(struct nalias), compare_by_index);
523534
{
524535
/* Process all aliases with the same encoding_index together. */
525536
size_t j;

0 commit comments

Comments
 (0)