1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
<?xml version="1.0" encoding="utf-8"?>
<section xmlns="http://docbook.org/ns/docbook"
xmlns:xl="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
xml:id="archs.amd64">
<title>AMD64/EM64T</title>
<section>
<title>Position Independent Code Issues</title>
<para>
Gentoo Policy demands all shared objects to be compiled with <command>-fPIC</command>
in <command>CFLAGS</command>. Since this is only a rule, you <emphasis>can</emphasis> break it on some arches.
You might never notice it. On AMD64, this is a necessity, if shared objects aren't
built with support for position independent code, the build process bails out
with an error message like this:
</para>
<programlisting>
foo.o: relocation R_X86_64_32 can not be used when making a shared
object; recompile with -fPIC
</programlisting>
</section>
<section>
<title>How to fix -fPIC issues</title>
<para>
There are several ways to enforce <command>-fPIC</command> on shared objects, each with its
pros and cons.
</para>
<section>
<title xmlns="http://docbook.org/ns/docbook"><command>sed</command>'ing the Makefile</title>
<para xmlns="http://docbook.org/ns/docbook">
Sometimes, a simple <command>sed</command> command is enough to fix it, however, the statements
normally aren't very readable and may fail when upstream changes the file.
Please verify that you only change the <command>CFLAGS</command> for <emphasis>shared</emphasis> objects, not for
the whole package.
</para>
</section>
<section>
<title>Patching <command>Makefile.in</command>/<command>configure</command></title>
<para xmlns="http://docbook.org/ns/docbook">
This is more readable, and easier to send upstream.
</para>
</section>
</section>
<section>
<title>How <emphasis>not</emphasis> to fix -fPIC issues</title>
<para>
Do <emphasis>not</emphasis> patch the <command>Makefile</command> itself, since it is usually generated by the
<command>configure</command> script and may vary heavily, so the patch could fail. Also, this
doesn't help upstream at all.
</para>
<para>
Another bad idea is to (ab)use <command>append-flags</command> function from
<command>flag-o-matic.eclass</command>. Applying <command>-fPIC</command> on all objects should not be
done. It should only be applied to shared objects.
</para>
</section>
<section>
<title>Assembler Optimisation</title>
<para>
Modern x86 processors support special instruction sets like <command>mmx</command>, <command>sse</command>,
<command>SSE2</command> and <command>3DNow!</command>. AMD64 also provides support for them, but in most
cases, x86 assembler code is incompatible with AMD64 assembler. There are lots
of packages that provide support through USE flags for these instruction sets.
Originally, the USE flags were introduced to keep support for older processors
such as the Pentium I that can't handle such code. Currently, all AMD64s support the
same combination of extended instruction sets, so there is no reason to make
use of the mentioned USE flags. That's why these USE flags are hard-masked in
all AMD64-profiles. This doesn't mean we don't support the extensions
themselves, instead, we hard-enable them.
</para>
<para>
The following USE flags are masked on AMD64:
</para>
<itemizedlist>
<listitem><para>
mmx
</para></listitem>
<listitem><para>
mmx2
</para></listitem>
<listitem><para>
sse
</para></listitem>
<listitem><para>
sse2
</para></listitem>
<listitem><para>
3dnow
</para></listitem>
<listitem><para>
3dnowext
</para></listitem>
</itemizedlist>
</section>
<section>
<title>Multilib on AMD64</title>
<para>
The current AMD64 processors are able to natively run 32bit code on a 64bit
kernel. Therefore, you can run programs compiled for x86 in an amd64 environment.
However, 32bit applications need to be linked against 32bit libraries. Mixing
them won't work. For this reason the libraries are sorted, 32bit libraries normally
go to <command>/lib32</command> respectively <command>/usr/lib32</command>, the 64bit ones normally to <command>/lib64</command> or
<command>/usr/lib64</command>. In a perfect world, you wouldn't have to read on. Unfortunately,
that's not the case, and so it's a bit more complicated.
</para>
<section>
<title>Multilib-Toolchain</title>
<section>
<title xmlns="http://docbook.org/ns/docbook">GCC</title>
<para xmlns="http://docbook.org/ns/docbook">
To generate 32bit code, we need a multilib-capable GCC. On other architectures,
this functionality is enabled with the USE flag <command>multilib</command>. This is also true
for amd64 with the <emphasis>pre</emphasis>-2005.0 profiles. From 2005.0 on, you have to choose
whether you want multilib support or not by selecting the profile. Choose
<command>2005.0/no-multilib</command> if you don't want it, all other profiles have the
<command>multilib</command> USE flag masked, you're forced to it. With these profiles, GCC will
produce x86-code whenever you add <command>-m32</command> to its command line. Adding <command>-m64</command>
or omitting any bit-width option will default to producing 64bit code.
</para>
</section>
<section>
<title>glibc</title>
<para xmlns="http://docbook.org/ns/docbook">
If you've chosen a multilib profile, glibc will be built twice, once 64bit and
once 32bit. This is because nearly every application links against glibc.
To understand how this is done in the ebuild, read
<xref linkend="archs.amd64.#The ABI Variable"/>.
</para>
</section>
</section>
<section>
<title>The <command>emul-linux-x86-*</command> packages</title>
<para>
As you read above, 32bit applications must be linked against 32bit libraries.
For that, we've put together the most used libraries and stuck them into the
so-called <command>emul-linux-x86</command> packages, which are located in the
<command>app-emulation</command> category.
</para>
<table>
<title>Emul-* Libraries</title><tgroup cols="2"><tbody>
<row><entry> emul-linux-x86-baselibs</entry>
<entry>Contains very basic libraries like zlib, pam, ncurses.</entry>
</row>
<row><entry>emul-linux-x86-compat</entry>
<entry>Contains very old libraries for compatibility with binary-only programs.</entry>
</row>
<row><entry>emul-linux-x86-glibc</entry>
<entry>Only used for compatibility reasons. This package is blocked by all 2005.0 profiles.</entry>
</row>
<row><entry>emul-linux-x86-gtklibs</entry>
<entry>The name says it — GTK and its engines belong herein.</entry>
</row>
<row><entry>emul-linux-x86-nvidia</entry>
<entry>Obsolete, <command>media-video/nvidia-glx</command> includes the 32bit library.</entry>
</row>
<row><entry>emul-linux-x86-qtlibs</entry>
<entry>QT goes here.</entry>
</row>
<row><entry>emul-linux-x86-sdl</entry>
<entry>libsdl and friends are here.</entry>
</row>
<row><entry>emul-linux-soundlibs</entry>
<entry>alsa, libogg, just everything that is needed to get sound is located here.</entry>
</row>
<row><entry>emul-linux-x86-xlibs</entry>
<entry>Contains the basic X libraries.</entry>
</row>
</tbody></tgroup></table>
<para>
These packages only provide pre-compiled libraries. Currently, the
archives are assembled manually, which is the main reason to keep the packages
as tidy as possible. Do not include libraries that aren't commonly used.
</para>
<note><para>
The emul-packages might conflict with their native images, but only in
uncritical locations like <command>/usr/share</command> which are arch-independent anyway.
</para></note>
</section>
<section>
<title><command>Libdir</command> Links</title>
<para>
Currently, we provide several profiles, each with its own combination of <command>libdir</command>
configurations.
</para>
<table><title> ... </title><tgroup cols="4"><thead><row>
<entry>
Profile
</entry>
<entry>
lib32
</entry>
<entry>
lib
</entry>
<entry>
lib64
</entry>
</row></thead><tbody><row>
<entry>
2004.3
</entry>
<entry>
*l->emul*
</entry>
<entry>
d64
</entry>
<entry>
*l->lib*
</entry>
</row><row>
<entry>
2004.3/lib64
</entry>
<entry>
*l->emul*
</entry>
<entry>
*l->64*
</entry>
<entry>
d64
</entry>
</row><row>
<entry>
>=2005.0
</entry>
<entry>
d32
</entry>
<entry>
*l->64*
</entry>
<entry>
d64
</entry>
</row><row>
<entry>
>=2005.0/no-multilib
</entry>
<entry>
d32
</entry>
<entry>
*l->64*
</entry>
<entry>
d64
</entry>
</row><row>
<entry>
>=2005.0/no-symlink
</entry>
<entry>
d32
</entry>
<entry>
d
</entry>
<entry>
d64
</entry>
</row><row>
<entry>
>=2005.0/no-symlink/no-lib32
</entry>
<entry>
inexistant
</entry>
<entry>
d32
</entry>
<entry>
d64
</entry>
</row></tbody></tgroup></table>
<para>
<emphasis>d</emphasis>: Directory containing mixed-bit objects
<emphasis>dXX</emphasis>: Directory containing XXbit objects
<emphasis>l->foo</emphasis>: Link to foo
</para>
<para>
To always get the right path, you should use the function <command>$(get_libdir)</command> from
<command>multilib.eclass</command>. It will always return the correct directory, on all arches.
And of course it also takes care of the <command>ABI</command> variable.
</para>
</section>
<section>
<title>The <command>multilib-strict</command> Feature</title>
<para>
Many Makefiles assume that their libraries should go to <command>/usr/lib</command>, or
<command>$(prefix)/lib</command>. This assumption can cause a serious mess if <command>/usr/lib</command>
isn't a symlink to <command>/usr/lib64</command>. To find the bad packages, we have a portage feature
called <emphasis>multilib-strict</emphasis>. It will prevent emerge from putting 64bit libraries
into anything other than <command>(/usr)/lib64</command>.
</para>
<para>
<command>multilib-strict</command> currently doesn't check perl5, gcc, gcc-lib and eclipse-3,
this behaviour is controlled by the <command>MULTILIB_STRICT_EXEMPT</command> variable in
<command>make.profile</command>.
</para>
<section>
<title>How to fix ebuilds properly</title>
<para xmlns="http://docbook.org/ns/docbook">
In most cases, it's sufficient to use the <command>$(get_libdir)</command> function from
<command>multilib.eclass</command>:
</para>
<programlisting xmlns="http://docbook.org/ns/docbook" language="ebuild">
inherit multilib
src_compile() {
econf \
--libdir=/usr/$(get_libdir)
emake || die
}
src_install() {
emake DESTDIR="${D}" install || die
}
</programlisting>
<para xmlns="http://docbook.org/ns/docbook">
Some packages provide really bad Makefiles which hard-code <command>/usr/lib</command>. Those
should be <command>sed</command> -ed or patched. Don't forget to let upstream know about your
modifications!
</para>
</section>
</section>
<section>
<title>Headers and Multilib</title>
<para>
Most C/C++ programs need standard header files like <command>types.h</command>. Some of them
depend on architecture specific facts, e.g. <command>types.h</command> on the length
of machine words. To ensure that we can compile both 32bit and 64bit
applications and libraries, we treat <command>/usr/include/asm</command> a bit special.
</para>
<para>
This is what <command>/usr/include/asm/types.h</command> looks like on a AMD64 box:
</para>
<programlisting language="c">
/* Common header file autogenerated by create_ml_includes in multilib.eclass */
#ifdef __i386__
#include <asm-i386/types.h>
#endif /* __i386__ */
#ifdef __x86_64__
#include <asm-x86_64/types.h>
#endif /* __x86_64__ */
</programlisting>
<para>
As you can see, this is just a wrapper that decides which file you need
depending on the the parameter <command>-D</command> given to gcc. You'll probably run into
some troubles if you try to compile something by hand and forget to append
<command>-D__x86_64__</command> to your <command>CFLAGS</command>. Of course, this is <emphasis>not necessary</emphasis> when
using portage. For an explanation, see the <xref linkend="archs.amd64.#The ABI Variable"/>
section.
</para>
</section>
<section>
<title>The ABI Variable</title>
<para>
Whenever portage builds something on amd64, it has to decide whether it should
be 32bit or 64bit. As stated in <xref linkend="archs.amd64.#Headers and Multilib"/>
the <command>__i386__</command> or <command>__x86_64__</command> respectively, is needed in <command>CDEFINE</command>.
Also, gcc has to know what code it should produce, therefore <command>-m32</command> or <command>-m64</command>
must be appended to CFLAGS. This is done via <command>profile.bashrc</command>. All you need to do
if you want to build a package 32bit is to set <command>ABI=x86</command>.
</para>
<para>
The details are shown in <command>make.defaults</command>:
</para>
<programlisting language="ebuild">
MULTILIB_ABIS="x86 amd64"
DEFAULT_ABI="amd64"
CFLAGS_amd64="-m64"
LDFLAGS_amd64="-m elf_x86_64"
CHOST_amd64="x86_64-pc-linux-gnu"
CDEFINE_amd64="__x86_64__"
LIBDIR_amd64="lib64"
CFLAGS_x86="-m32 -L/emul/linux/x86/lib -L/emul/linux/x86/usr/lib"
LDFLAGS_x86="-m elf_i386 -L/emul/linux/x86/lib -L/emul/linux/x86/usr/lib"
CHOST_x86="i686-pc-linux-gnu"
CDEFINE_x86="__i386__"
LIBDIR_x86="lib32"
</programlisting>
</section>
</section>
<section>
<title>Porting Notes</title>
<section>
<title>Machine Word sizes</title>
<para>
On AMD64, some types differ in size from x86:
</para>
<table><title> ... </title><tgroup cols="3"><thead><row>
<entry>
Type
</entry>
<entry>
x86 (ILP32)
</entry>
<entry>
amd64 (LP64)
</entry>
</row></thead><tbody><row>
<entry>
<command>char</command>
</entry>
<entry>
1 byte
</entry>
<entry>
1 byte
</entry>
</row><row>
<entry>
<command>short</command>
</entry>
<entry>
2 bytes
</entry>
<entry>
2 bytes
</entry>
</row><row>
<entry>
<command>int</command>
</entry>
<entry>
4 bytes
</entry>
<entry>
4 bytes
</entry>
</row><row>
<entry>
<command>long</command>
</entry>
<entry>
<emphasis>4 bytes</emphasis>
</entry>
<entry>
<emphasis>8 bytes</emphasis>
</entry>
</row><row>
<entry>
<command>long long</command>
</entry>
<entry>
8 bytes
</entry>
<entry>
8 bytes
</entry>
</row><row>
<entry>
<command>pointer</command>
</entry>
<entry>
<emphasis>4 bytes</emphasis>
</entry>
<entry>
<emphasis>8 bytes</emphasis>
</entry>
</row><row>
<entry>
<command>float</command>
</entry>
<entry>
4 bytes
</entry>
<entry>
4 bytes
</entry>
</row><row>
<entry>
<command>double</command>
</entry>
<entry>
8 bytes
</entry>
<entry>
8 bytes
</entry>
</row><row>
<entry>
<command>long double</command>
</entry>
<entry>
16 bytes
</entry>
<entry>
16 bytes
</entry>
</row></tbody></tgroup></table>
<para>
If you need an exact amount of space, don't use these types but the <command>uXX</command> and
<command>sXX</command> ones provided by <command>types.h</command>, where XX is the number of bits you need.
Switching to a type that is the same on both arches is not suggested since it's
not a clean solution and might cause problems with other arches.
</para>
</section>
<section>
<title>Casts</title>
<para>
Many upstream developers assume that the length of a pointer is 4 bytes, which
can cause problems when programs do casts from <command>void *</command> to <command>int</command> and vice
versa. With GCC 3.4, this causes warnings, the compilation won't abort. If
you're lucky, your package works, but it's likely that you encounter
segmentation faults or strange behaviour. GCC 4.0 refuses to compile such code.
</para>
</section>
</section>
<section>
<title>Other Resources</title>
<itemizedlist>
<listitem><para>
<link xl:href="http://amd64.gentoo.org/">Gentoo/AMD64 Project</link>
</para></listitem>
<listitem><para>
<link xl:href="http://www.gentoo.org/doc/en/gentoo-amd64-faq.xml">Gentoo/Linux AMD64 FAQ</link>
</para></listitem>
<listitem><para>
<link xl:href="http://forums.gentoo.org/viewforum-f-46.html">Gentoo on AMD64 Forum</link>
</para></listitem>
</itemizedlist>
</section>
</section>
|