MichaelMichael TiemannTiemann CTOCTO RedRed Hat,Hat, Inc.Inc.

FebruaryFebruary 16,16, 20002000 AgendaAgenda l Trillian,Trillian, GNUGNU andand CygnusCygnus // RedRed HatHat l PortingPorting toto IAIA--6464 LinuxLinux ––IAIA--6464 LinuxLinux (Your(Your Target)Target) CharacteristicsCharacteristics ––KnowKnow YourYour StartingStarting PointPoint ––PortingPorting Issues/ChecklistIssues/Checklist ––UsingUsing GNUGNU ToolsTools toto EaseEase PortingPorting – other helpful tools l CallCall toto actionaction What’sWhat’s GNU?GNU? l GGNUNU isis NNotot UUnix!nix! l NonNon--proprietary,proprietary, cooperativecooperative developmentdevelopment ofof tools,tools, applicationsapplications andand systemssystems – very efficient at creating veryvery goodgood peer- reviewed software – identical tools across many computer systems – naturally promotes portable software l OpenOpen sourcesource – implementations are publicly readable – excellent learning tool GNUGNU toolstools areare usedused byby 95%95% ofof allall LinuxLinux developers!!developers!! SomeSome GNUGNU HistoryHistory 1984: GNU project starts - emacs 1985: GNU Debugger GDB 1987: GCC and G++

1989: First commercial GNU tools 1991: GCC 2.0, GDB 4.0 2000 1985 1990 1995

2000: GCC for IA-64, GCC 3.0, GDB 5.0 RedRed HatHat (C(Cygygnus)nus) TrillianTrillian EffortEffort l IAIA--6464 workwork startedstarted withwith HP’sHP’s initialinitial portport ofof GCCGCC compilercompiler inin May’99May’99 l ContributedContributed GNUProGNUPro componentscomponents forfor thethe TrillianTrillian efforteffort ––gccgcc,, g++,g++, gas,gas, gldgld ––ProvidesProvides bothboth nativenative andand crosscross developmentdevelopment environmentsenvironments ––OnlyOnly LP64LP64 datadata modelmodel supportedsupported ––SupportsSupports developmentdevelopment ofof EFIEFI applicationsapplications IAIA--6464 GNUGNU toolstools –– FutureFuture WorkWork l What’sWhat’s inin thethe worksworks ––ExceptionException handlinghandling supportsupport ––AssemblerAssembler dependencydependency violationviolation supportsupport (DVLOC)(DVLOC) ––IntrinsicsIntrinsics (efficiency of assembly within C/C++ code) ––IAIA--6464 --specificspecific AssemblerAssembler directivesdirectives ––FullFull gdbgdb supportsupport IAIA--6464 GNUGNU toolstools –– FutureFuture WorkWork l CompilerCompiler OptimizationsOptimizations plannedplanned ––NewNew hardwarehardware pipelinepipeline modelmodel ––SchedulingScheduling ofof instructionsinstructions latencieslatencies ––BetterBetter looploop awareaware aliasalias analysisanalysis ––SoftwareSoftware pipeliningpipelining ––PredicationPredication ––SpeculationSpeculation IAIA--6464 UNIXUNIX ABIABI WorkWork l Intel,Intel, SCO,SCO, HP,HP, IBM,IBM, SGI,SGI, SUN,SUN, RedRed Hat,Hat, VAVA LinuxLinux l AgreementsAgreements onon – Common object format - ELF and DWARF – Standardized ELF extensions for better code generation – String section – allows compressions of string table program wide – Extended number of sections – more optimizations to reduce program size – Standardized intrinsic functions to allow access processor features through from C/C++ programs – more… IAIA--6464 C++C++ ABIABI WorkWork l IAIA--6464 C++C++ ABIABI GroupGroup ––SGI,SGI, Intel,Intel, HP,HP, IBM,IBM, SUN,SUN, RedRed HatHat andand SCOSCO l AgreementsAgreements onon ––standardizedstandardized objectobject layoutslayouts ––commoncommon virtualvirtual tabletable implementationimplementation ––standardizedstandardized (and(and optimized)optimized) namename manglingmangling l AllowsAllows somesome C++C++ binariesbinaries fromfrom oneone UnixUnix versionversion toto anotheranother UNIXUNIX versionversion forfor IAIA--6464 platformsplatforms PortingPorting toto IAIA--6464 LinuxLinux EssenceEssence ofof IAIA--6464 LinuxLinux

IAIA--6464 LinuxLinux asas seenseen byby youryour code:code: l LP64LP64 – Like other 64-bit UNIX machines – ILP32 onlyonly via execution of x86 binaries – Caution: x86 and IA-64 processes can share data via shared memory, files, sockets, ... l StandardsStandards compliantcompliant (mostly)(mostly) – ISO/ANSI C, C++ (ISO Standards C90 and C99) – most UNIX/98 APIs EssenceEssence ofof IAIA--6464 LinuxLinux l littlelittle--endianendian (by(by default)default) ––nono bigbig--endianendian supportsupport l C/C++C/C++ prepre--definesdefines youyou cancan use:use: – established at compile time: __unix, __linux, __ia64, __LP64, __ELF__, __GNUC__ – established by header #include files: __WORDSIZE (== 64) KnowKnow YourYour SourceSource CodeCode l IsIs youryour sourcesource ISO/ANSIISO/ANSI compliant?compliant? l WhatWhat datadata structuresstructures areare externallyexternally visiblevisible (files,(files, sockets,sockets, sharedshared memory,memory, pipes,pipes, ...)...) toto otherother architectures?architectures? l WhatWhat platformplatform portsports areaarea alreadyalready done?done? l IsIs youryour sourcesource toto bebe builtbuilt onon bothboth UNIXUNIX andand Windows?Windows? PortingPorting toto IAIA--6464 LinuxLinux l IfIf youyou areare startingstarting fromfrom Windows*…Windows*… ––SetSet goalsgoals forfor LinuxLinux functionalityfunctionality ––PickPick toolstools andand toolkitstoolkits forfor LinuxLinux ––UseUse CC runtimeruntime librarylibrary andand POSIXPOSIX--likelike functionsfunctions insteadinstead ofof proprietaryproprietary MSMS interfacesinterfaces – Can use cygwin.dll as a bridge on IA-32 Porting to IA-32 Linux first is recommended, but you can port to IA-64 Linux directly

* All trademarks and brands are the property of their respective owners PortingPorting toto IAIA--6464 LinuxLinux l Guidelines:Guidelines: – Involves mostly recompiling if your source is already 64-bit clean – common object file format: ELF. All Unices will use it (no XCOFF, no SOM) – standardized ELF extensions allow better object generation. E.g. – the string section will allow compression of string table program-wide – the extended number of sections will allow putting functions (esp C++) in different sections – common binary format allows to develop tools usable for all different Unix platforms PortingPorting toto IAIA--6464 LinuxLinux l StepStep 1:1: GetGet GNUProGNUPro forfor IAIA--3232 LinuxLinux l StepStep 2:2: PortPort appsapps toto IAIA--3232 LinuxLinux ––UseUse -Wall -Wconversion -Wsign-compare l StepStep 3:3: CleanupCleanup ––pointer/integerpointer/integer warningswarnings ––signed/unsignedsigned/unsigned warningswarnings l StepStep 4:4: ReconfigureReconfigure andand recompilerecompile ChallenChallenggee ofof PortablePortable CodeCode l LegacyLegacy codecode oftenoften ignoresignores portabilityportability issuesissues l ManyMany programmersprogrammers areare simplysimply notnot taughttaught toto writewrite portableportable softwaresoftware l WritingWriting andand maintainingmaintaining portableportable softwaresoftware isis challenging,challenging, butbut worthworth itit

GNUGNU SoftwareSoftware isis exemplary…exemplary… learnlearn fromfrom it!it! WritingWriting PortablePortable SoftwareSoftware l FollowFollow GNUGNU guidelinesguidelines ––WriteWrite codecode toto bebe ableable toto compilecompile withwith ISOISO CC oror ISOISO C++C++ compilerscompilers ––ConfigurationConfiguration // supportsupport packagespackages forfor compatibilitycompatibility – autoconf, automake, libtool l Example:Example: GNUGNU ‘hello’‘hello’ ––GNUGNU codingcoding standardsstandards ––InternationalizationInternationalization supportsupport PortingPorting RulesRules forfor IAIA--6464 l DODO ––UseUse #include#include toto getget systemsystem datadata typestypes l DON’TDON’T ––declaredeclare thingsthings youyou don'tdon't definedefine (required(required byby ISOISO C)C) l NEVERNEVER ––declaredeclare aa structurestructure thethe systemsystem definesdefines PortingPorting ChecklistChecklist l FixFix castscasts thatthat cancan truncatetruncate (int)&xyz, (long)&xyz ⇒ (uintptr_t)&xyz l UseUse sizeofsizeof()() andand offsetofoffsetof()() ––hardhard--codedcoded numbersnumbers -- ““44”” -- don’tdon’t portport ––n.b.n.b. argsargs toto mallocmalloc()(),, pointerpointer arithmeticarithmetic l UseUse size_tsize_t insteadinstead ofof intint oror longlong int ln= strlen(…); ⇒ size_t ln= str… ––otherother revisedrevised APIsAPIs PortingPorting ChecklistChecklist l UseUse sizedsized typestypes forfor externalexternal structuresstructures ––ISO/ANSIISO/ANSI datadata typestypes struct on_disk { int reclen; … ⇒ struct on_disk { int32_t reclen; … ––alsoalso forfor “on“on--wire”wire” andand sharedshared memorymemory structstructss l AccommodateAccommodate packingpacking differencesdifferences ––IAIA--6464 defaultsdefaults toto “natural“natural alignment”alignment” ––useuse ##pragmapragmass forfor sharedshared legacylegacy structuresstructures PortingPorting ChecklistChecklist l ReviseRevise printfprintf()(),, scanfscanf()() specifiersspecifiers “%08lX”,&foo) ⇒ (“%p”,…; “%d” ⇒ “%ld”; … l #error#error casecase atat endend ofof ##ifdefifdef chainschains ––preventsprevents unexpected/silentunexpected/silent useuse ofof somesome defaultdefault casecase … #elif defined(__ia64) … #else #error Update for new platform! #endif PortingPorting ChecklistChecklist l UseUse APIAPI callscalls toto getget systemsystem paramsparams getpagesize(), sysconf(_SC_CLK_TICK) l UseUse headerheader filefile mnemonicsmnemonics SEEK_END (notnot 2), INT_MIN (notnot -2147483648)) l InlineInline assemblyassembly mustmust bebe rere--writtenwritten OtherOther thingsthings youyou cancan dodo l SortSort youryour structuresstructures – Always optimize for speed and not space – Put the structure elements together that are used often to find them in the same cache line – You should also – Put pointers together – Put shorts together – Put chars together – Net result: improved structure packing l --WpaddedWpadded – warn when gcc pads to alignment boundary PortingPorting withwith GNUProGNUPro ToolsTools l CompilersCompilers enhancedenhanced toto checkcheck forfor typetype compatibilitycompatibility problemsproblems ––sizesize incompatibilitiesincompatibilities ––alignment/paddingalignment/padding messagesmessages ––badbad comparisonscomparisons l BothBoth nativenative andand crosscross compilerscompilers availableavailable SampleSample (Buggy)(Buggy) ProgramProgram int bugs (int foo, unsigned bar) { signed int si; unsigned int ui; si = -1; ui = ~0; ui = ~0U; ui = ~0UL; if (si < ui) return 1; return -1; } int main (int argc, char **argv) { int i; int *p = &i; bugs (p, i); bugs ((int)p, -1); return 0; } GNUProGNUPro toolstools onon IAIA--3232 tiemann@happy$ more ../linux/config.status #!/bin/sh # This file was generated automatically by configure. Do not edit. # This directory was configured as follows: /work/devo/configure --with-gcc-version-trigger=/work/devo/gcc/version.c --host=i686-pc-linux- -v --srcdir=/work/devo --norecursion # using "mh-frag" and "mt-frag" tiemann@happy$ gcc -Wall -Wsign-compare -Wconversion bugs.c -S bugs.c: In function `bugs': bugs.c:7: warning: negative integer implicitly converted to unsigned type bugs.c:10: warning: comparison between signed and unsigned bugs.c: In function `main': bugs.c:19: warning: passing arg 1 of `bugs' makes integer from pointer without a cast bugs.c:19: warning: passing arg 2 of `bugs' as unsigned due to prototype bugs.c:20: warning: passing arg 2 of `bugs' as unsigned due to prototype bugs.c:20: warning: negative integer implicitly converted to unsigned type GNUProGNUPro toolstools crosscross IAIA--6464 tiemann@happy$ more ../linux-linux64/config.status #!/bin/sh # This file was generated automatically by configure. Do not edit. # This directory was configured as follows: /work/devo/configure --with-gcc-version-trigger=/work/devo/gcc/version.c --host=i686-pc-linux-gnu --target=ia64-linux-gnu --srcdir=/work/devo --norecursion # using "mt-frag" tiemann@happy$ ../linux-linux64/gcc/xgcc -Wsign-compare -Wconversion bugs.c -S bugs.c: In function `bugs': bugs.c:7: warning: negative integer implicitly converted to unsigned type bugs.c:9: warning: large integer implicitly truncated to unsigned type bugs.c:10: warning: comparison between signed and unsigned bugs.c: In function `main': bugs.c:19: warning: passing arg 1 of `bugs' makes integer from pointer without a cast bugs.c:19: warning: passing arg 2 of `bugs' as unsigned due to prototype bugs.c:20: warning: cast from pointer to integer of different size bugs.c:20: warning: passing arg 2 of `bugs' as unsigned due to prototype bugs.c:20: warning: negative integer implicitly converted to unsigned type GNUProGNUPro toolstools onon IAIA--6464 bash-2.03$ /usr/cygnus/bin/gcc -Wall -Wsign-compare -Wconversion bugs.c bugs.c: In function `bugs': bugs.c:7: warning: negative integer implicitly converted to unsigned type bugs.c:9: warning: large integer implicitly truncated to unsigned type bugs.c:10: warning: comparison between signed and unsigned ··· ··· -- the same thorough diagnostics as when using the cross-compiler ··· bash-2.03$ gdb a.out GNU gdb 4.18.1 Copyright 1998 Foundation, Inc. ··· This GDB was configured as "ia64-pc-linux"... PortabilityPortability WarningsWarnings l Integer/PointerInteger/Pointer assumptionsassumptions ––typetype sizesize ––RoundtripRoundtrip conversionconversion ((intint-->void*>void* -->>intint)) l Signed/UnsignedSigned/Unsigned mismatchesmismatches ––ComparisonsComparisons ––ConversionsConversions l ShiftsShifts andand MasksMasks ––ManyMany hashhash algorithmsalgorithms assumeassume 3232--bitbit intsints!! WhatWhat MakesMakes PortingPorting Easy?Easy? l GNUGNU--basedbased compilerscompilers areare usedused forfor 99%99% ofof allall LinuxLinux developmentdevelopment l TheThe LinuxLinux APIsAPIs areare cleanclean – IA-64 Linux is a pure 64-bit model – No legacy 32-bit complexity l GNUProGNUPro compilerscompilers havehave specialspecial featuresfeatures toto helphelp portport codecode – Between big and little endian – Across different integer and pointer sizes – To systems with different alignment rules CallCall toto ActionAction l GetGet thethe IAIA--6464 LinuxLinux OSOS kernelkernel ––AvailableAvailable atat www.ia64linux.orgwww.ia64linux.org l GetGet GNUProGNUPro forfor IAIA--3232 andand IAIA--6464 ––SeeSee http://www.http://www.cygnuscygnus.com/.com/gnuprognupro// andand http://www.http://www.cygnuscygnus.com/.com/gnuprodevkitgnuprodevkit// l AdaptAdapt youryour buildbuild environmentenvironment toto configureconfigure and/orand/or autoconfautoconf ––ThisThis makesmakes itit easyeasy toto maintainmaintain aa singlesingle sourcesource basebase forfor IAIA--3232 andand IAIA--6464 targetstargets AdditionalAdditional InformationInformation l SeeSee http://www.http://www.fsffsf.org/software/.org/software/ listslists GNUGNU software,software, includingincluding hellohello l VisitVisit gccgcc.gnu.org.gnu.org ifif youyou wantwant toto contributecontribute toto thethe compilercompiler workwork l http://developer.http://developer.intelintel.com/design/.com/design/iaia--6464 isis aa resourceresource forfor additionaladditional IAIA--6464 informationinformation