Affichage des articles dont le libellé est PowerPC. Afficher tous les articles
Affichage des articles dont le libellé est PowerPC. Afficher tous les articles

samedi 2 février 2008

Une commande Mac OS X peu connue: machine

Il y a quelques jours je vous parlais de la commande arch de Mac OS X.

Voyons aujourd'hui une autre commande peu connue, la commande machine.

Cette commande est un peu similaire à la commande arch dont je vous parlais l'autre fois. En fait la commande machine affiche le type de machine sur laquelle Mac OS X tourne.

Attention, l'architecture et le type de machine, ce n'est pas la même chose !

L'architecture, donnée par la commande arch, est ppc, ppc64, i386 ou x86_64.

Le type de machine, donnée par la commande machine, indique avec précision le type de processeur.

Exemple avec Mac OS X Tiger sur un Power Macintosh G3 Blue and white:
[pierre@blueandwhite] /Users/pierre>machine
ppc750
Exemple avec Mac OS X Léopard sur un iMac Intel 20-inch:
[pierre@albert] /Users/pierre>machine
i486

Voyons les pages de manuel de cette commande machine:

Mac OS X Tiger sur PowerPC:
MACHINE(1)                BSD General Commands Manual               MACHINE(1)

NAME
machine -- print machine type

SYNOPSIS
machine

DESCRIPTION
The machine command displays the machine type.

SEE ALSO
make(1)

HISTORY
The machine command is currently under development.

BSD July 26, 1991 BSD
Mac OS X Léopard sur Intel:
MACHINE(1)                BSD General Commands Manual               MACHINE(1)

NAME
machine -- print machine type

SYNOPSIS
machine

DESCRIPTION
The machine command displays the machine type.

SEE ALSO
arch(1), make(1)

HISTORY
The machine command is currently under development.

BSD July 26, 1991 BSD
... on constatera au passage que cette commande est toujours "currently under development", étonnant :)

mercredi 30 janvier 2008

Une commande Unix / Linux / Mac OS X peu connue: arch

Voyons aujourd'hui une commande Mac OS X peu connue: il s'agit de la commande arch.

Cette commande renvoie l'architecture de la machine.
Le résultat peut être:
  • ppc pour un Apple Macintosh basé sur un processeur PowerPC 32 bits (G3, G4, G5)
  • ppc64 pour un Apple Macintosh basé sur un processeur PowerPC 64 bits (G5)
  • i386 pour un Apple Macintosh basé sur un processeur x86 (Intel) 32 bits
  • x86_64 pour un Apple Macintosh basé sur un processeur x86_64 (Intel) 64 bits
  • ??? (à vérifier - probablement arm) pour un iPhone, qui fait tourner Mac OS X sur un processeur arm.

Voyons les pages de manuel de cette commande arch:

Mac OS X Tiger sur PowerPC:

ARCH(1) BSD General Commands Manual ARCH(1)

NAME
arch -- print architecture type

SYNOPSIS
arch

DESCRIPTION
The arch command displays the machine's architecture type.

SEE ALSO
machine(1)

Mac OS August 20, 1997 Mac OS

Mac OS X Leopard sur Intel:

ARCH(1) BSD General Commands Manual ARCH(1)

NAME
arch -- print architecture type or run selected architecture of a univer-
sal binary

SYNOPSIS
arch
arch [-h] [[-arch_name | -arch arch_name]...] prog [args ...]

DESCRIPTION
The arch command with no arguments, displays the machine's architecture
type.

The other use of the arch command it to run a selected architecture of a
universal binary. A universal binary contains code that can run on dif-
ferent architectures. By default, the operating system will select the
architecture that most closely matches the processor type. This means
that an intel architecture is selected on intel processors and a powerpc
architecture is selected on powerpc processors. A 64-bit architecuture
is preferred over a 32-bit architecture on a 64-bit processor, while only
32-bit architectures can run on a 32-bit processor.

When the most natural architecture is unavailable, the operating system
will try to pick another architecture. On 64-bit processors, a 32-bit
architecture is tried. If this is also unavailable, the operating system
on an intel processor will try running a 32-bit powerpc architecture.
Otherwise, no architecture is run, and an error results.

The arch command can be use to alter the operating system's normal selec-
tion order. The most common use is to select the 32-bit architecture on
a 64-bit processor, even if a 64-bit architecture is available.

The -h option prints a usage message and exits.

The arch_name argument must be one of the currently supported architec-
tures:

i386 32-bit intel

ppc 32-bit powerpc

ppc64 64-bit powerpc

x86_64 64-bit intel

Either prefix the architecture with a hyphen, or (for compatibility with
other commands), use -arch followed by the architecture.

If more than one architecture is specified, the operating system will try
each one in order, skipping an architecture that is not supported on the
current processor, or is unavailable in the universal binary.

The prog argument is the command to run, followed by any arguments to
pass to the command. It can be a full or partial path, while a lone name
will be lookup in the user's command search path.

If no architectures are specified on the command line, the arch command
takes the basename of the prog argument and searches for the first prop-
erty list file with that basename and the .plist suffix, in the
archSettings sub-directory in each of the standard domains, in the fol-
lowing order:

~/Library/archSettings User settings

/Library/archSettings Local settings

/Network/Library/archSettings Network settings
Either prefix the architecture with a hyphen, or (for compatibility with
other commands), use -arch followed by the architecture.

If more than one architecture is specified, the operating system will try
each one in order, skipping an architecture that is not supported on the
current processor, or is unavailable in the universal binary.

The prog argument is the command to run, followed by any arguments to
pass to the command. It can be a full or partial path, while a lone name
will be lookup in the user's command search path.

If no architectures are specified on the command line, the arch command
takes the basename of the prog argument and searches for the first prop-
erty list file with that basename and the .plist suffix, in the
archSettings sub-directory in each of the standard domains, in the fol-
lowing order:

~/Library/archSettings User settings

/Library/archSettings Local settings

/Network/Library/archSettings Network settings
Either prefix the architecture with a hyphen, or (for compatibility with
other commands), use -arch followed by the architecture.

If more than one architecture is specified, the operating system will try
each one in order, skipping an architecture that is not supported on the
current processor, or is unavailable in the universal binary.

The prog argument is the command to run, followed by any arguments to
pass to the command. It can be a full or partial path, while a lone name
will be lookup in the user's command search path.

If no architectures are specified on the command line, the arch command
takes the basename of the prog argument and searches for the first prop-
erty list file with that basename and the .plist suffix, in the
archSettings sub-directory in each of the standard domains, in the fol-
lowing order:

~/Library/archSettings User settings

/Library/archSettings Local settings

/Network/Library/archSettings Network settings

/System/Library/archSettings System settings

This property list contains the architecture order preferences, as well
as the full path to the real executable. For examples of the property
list format, look at the files in /System/Library/archSettings.

Example
On an intel processor:

% perl -MConfig -e 'printf "%s\n", $Config{byteorder}'
1234

shows the intel little endian byte order, while:

% arch -ppc perl -MConfig -e 'printf "%s\n", $Config{byteorder}'
4321

runs the powerpc architecture, and displays big endian byte order.

Making links to the arch command
When a link is made to arch command with a different name, that name is
used to find the corresponding property list file. Thus, other commands
can be wrapped so that they have custom architecture selection order.
Because of some internal logic in the code, hard links to the arch com-
mand may not work quite right. It is best to avoid using hard links, and
only use symbolic links to the arch command.

Environment
The environment variable ARCHPREFERENCE can be used to provide architec-
ture order preferences. It is checked before looking for the correspond-
ing property list file.

The value of the environment variable ARCHPREFERENCE is composed of one
or more specifiers, separated by semicolons. A specifier is made up of
one, two or three fields, separated by colons. Architectures specified
in order, are separated by commas and make up the last (mandatory) field.
The first field, if specified, is a name of a program, which selects this
specifier if that name matches the program name in question. If the name
field is empty or there is no name field, the specifier matches any pro-
gram name. Thus, ordering of specifiers is important, and the one with
no name should be last.

When the arch command is called directly, the prog name provides the path
information to the executable (possibly via the command search path).
When a name is specified in a ARCHPREFERENCE specifier, the path informa-
tion can alternately be specified as a second field following the name.
When the arch command is called indirectly via a link, this path informa-
tion must be specified. If not specified as a second field in a speci-
fier, the executable path will be looked up in the corresponding property
list file.

Example ARCHPREFERENCE Values
ppc,i386,ppc64,x86_64
A specifier that matches any name.

foo:ppc,i386,ppc64,x86_64
A specifier that matches the program named foo (the full executable
path is in the foo.plist file).

foo:/op/bin/boo:ppc,i386,ppc64,x86_64
A specifier with all fields specified.

baz:ppc,i386;i386,ppc
A specifier for baz and a second specifier that would match any
other name.

BUGS
BUGS
Running the arch command on an interpreter script may not work if the
interpreter is a link to the arch command, especially if a 64-bit archi-
tecture is specified (since the arch command is 2-way universal, 32-bit
only).

SEE ALSO
machine(1)

Mac OS X November 12, 2006 Mac OS X


Il est à noter que sous Debian GNU/Linux, la commande arch existe dans Debian GNU/Linux 4.0 "etch", mais a disparu de Debian GNU/Linux "lenny", car elle est considérée "obsolète". Si vous en avez malgré tout besoin, utilisez la commande uname -m. Il vous suffit de créer un petit shell script /bin/arch du genre:

#!/bin/sh
#
# Remplace la commande arch
# Renvoie l'architecture de la machine
#
# Pierre Bauduin
# Le 24 décembre 2007
#
/bin/uname -m

Certains distributions Linux ont la commande arch, d'autres ne l'ont pas.

mercredi 9 janvier 2008

Comparons le header Mach-O sous powerpc et x86

Le format d'exécutable sous Mac OS X est le format Mach-O.

La commande otool -h permet d'analyser le header d'un fichier au format Mach-O.

Je lis à la page 12 de "Mac OS X ABI Mach-O File Format Reference" que le header du format Mach-O a toujours la structure suivante:
struct mach_header
{
uint32_t magic;
cpu_type_t cputype;
cpu_subtype_t cpusubtype;
uint32_t filetype;
uint32_t ncmds;
uint32_t sizeofcmds;
uint32_t flags;
};

Voyons le résultat de otool -h sur un fichier Mach-O bien connu (/bin/ls), mais sur deux architectures différentes:

Mac OS X Leopard sur Intel (x86):
[pierre@albert] /Users/pierre>otool -h /bin/ls
/bin/ls:
Mach header
magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
0xfeedface 7 3 0x00 2 14 1304 0x00000085

Mac OS X Tiger sur PowerPC (powerpc):
[pierre@blueandwhite] /Users/pierre>otool -h /bin/ls
/bin/ls:
Mach header
magic cputype cpusubtype filetype ncmds sizeofcmds flags
0xfeedface 18 0 2 11 1608 0x00000085
Regardez attentivement cputype: c'est lui qui nous indique sur quelle architecture nous sommes.

Voyez aussi la page man de la commande arch

En fait, le format Mach-O est directement hérité de NextStep, qui au long de son existence a été disponible pour plusieurs architectures: PowerPC, x86, PA-RISC et SPARC.

mardi 20 mars 2007

Comment créer un "Universal Binary" sous Mac OS X

Comme la gamme Apple est depuis 2006 "en transition" entre l'architecture PowerPC et l'architecture Intel, on peut se demander si tout ceci ne cause pas de problèmes.

En fait, grâce à l'architecture et aux outils de Mac OS X, cela se passe très bien.
Actuellement, un grand nombre de programmes sont distribués en "Universal Binaries", c'est à dire en bref qu'ils contiennent des exécutables PowerPC et Intel.
Quoiqu'il en soit, sur un Mac Intel sous Mac OS X, si vous essayez de lancer un exécutable PowerPC, il tournera de toutes façons gràce à Rosetta, le composant de Mac OS X qui traduit directement le code PowerPC en code Intel.

Il est intéressant de constater que sous Mac OS X, il est possible de créer un seul fichier exécutable qui contient les binaires pour les deux architectures PowerPC et Intel. Il s'agit en fait de "fat binaries" (qui sont appelées par Apple de l'euphémisme "universal binaries"...). Le concept de "fat binaries" est en fait relativement ancien, puisqu'il vient de NextStep, qui est un peu l'aïeul de Mac OS X. En fait dans le cas de Mac OS X les "fat binaries" sont une des fonctionnalités des exécutables Mach-O.

Regardez:
(Vous devez avoir installé XCode pour que ceci fonctionne)
Pour créer un "fat binary":
[pierre@ibook] /Users/pierre> gcc -arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk hello.c

Observons le résultat:
[pierre@ibook] /Users/pierre> file a.out
a.out: Mach-O fat file with 2 architectures
a.out (for architecture i386): Mach-O executable i386
a.out (for architecture ppc): Mach-O executable ppc


(Ceci a été effectué sur Mac OS X Tiger sur une machine PowerPC)

Le seul inconvénient de la migration de PowerPC vers Intel est que les applications "Classic" (c'est à dire, développées pour Mac OS 7.x, 8.x, 9.x) sont pour la poubelle. Enfin, c'est peut-être le bon endroit :).