Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

[インデックス 18096] ファイルの概要

このコミットは、Goプロジェクトのlibmachライブラリから、古くなったオブジェクトファイルリーダーを削除することを目的としています。これらのリーダーはもはや機能しておらず、それらを削除することで他のリファクタリングが容易になります。また、packツールにおける関連する機能も削除されます。

コミット

  • コミットハッシュ: 1334b794b74c535b0971ea51c940faa6b26fe184
  • Author: Russ Cox rsc@golang.org
  • Date: Fri Dec 20 12:10:53 2013 -0500

GitHub上でのコミットページへのリンク

https://github.com/golang/go/commit/1334b794b74c535b0971ea51c940faa6b26fe184

元コミット内容

libmach: remove old object file readers

These no longer work; removing them makes other refactoring easier.
The code for pack P being deleted in this CL does not work either.
I created issue 6989 to track restoring this functionality (probably not
until pack is written in Go).

R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/44300043

変更の背景

このコミットの主な背景は、Goプロジェクトのlibmachライブラリ内に存在していた古いオブジェクトファイルリーダーが、もはや正しく機能していなかったことです。これらのコードは、Goの進化に伴いメンテナンスされなくなり、現在のGoのオブジェクトファイル形式やツールチェインと互換性がなくなっていました。

機能しないコードを削除することで、以下の利点が得られます。

  1. コードベースの簡素化: 不要なコードがなくなることで、コードベースがスリムになり、理解しやすくなります。
  2. リファクタリングの促進: 古い依存関係や複雑なロジックが取り除かれることで、将来的なコードのリファクタリングや改善が容易になります。特に、コミットメッセージには「removing them makes other refactoring easier」と明記されており、これが重要な動機であったことが伺えます。
  3. packツールの再設計: packツールの一部機能もこれらの古いリーダーに依存しており、同様に機能していませんでした。このコミットでは、その機能も削除されます。将来的には、packツールがGo言語で再実装される際に、この機能が適切に復元されることが計画されています(Issue 6989で追跡)。これは、Goツールチェイン全体をGo言語で記述するというGoプロジェクトの長期的な目標の一環でもあります。

前提知識の解説

libmach

libmachは、Goプロジェクトの初期段階において、様々なアーキテクチャのオブジェクトファイル(コンパイルされたコードやデータを含むファイル)を読み取るためのライブラリとして機能していました。コミットで削除されるファイル名(5obj.c, 6obj.c, 8obj.c)から推測できるように、これらはそれぞれARM (5), AMD64 (6), 386 (8) アーキテクチャのオブジェクトファイルを扱うためのC言語で書かれたコードでした。

これらのファイルには、Inferno OSやPlan 9のコードベースからの影響が見られます。Go言語自体がPlan 9の思想やツールチェインから多くのインスピレーションを受けているため、初期のGoツールチェインにはこれらのシステムからのC言語コードが組み込まれていることがありました。libmachは、Goのリンカやその他のツールが、異なるアーキテクチャのオブジェクトファイルを解析し、シンボル情報などを抽出するために使用されていたと考えられます。

Goのオブジェクトファイル形式

Go言語のコンパイラは、ソースコードを中間表現(オブジェクトファイル)にコンパイルします。Goのオブジェクトファイル形式は、Goのツールチェイン内で使用される独自の形式であり、C/C++のELFやMach-O、PEなどの標準的なオブジェクトファイル形式とは異なります。これは、Goのリンカが効率的に動作し、クロスコンパイルを容易にするために設計されています。

packツール

packツールは、Goの初期のツールチェインの一部であり、Unixのar(archiver)コマンドに似た機能を提供していたと考えられます。arコマンドは、複数のオブジェクトファイルやライブラリファイルを一つのアーカイブファイルにまとめるために使用されます。Goのpackツールも、同様にGoのオブジェクトファイルをアーカイブ化する目的で使われていた可能性があります。コミットメッセージでpackツールが言及されていることから、packlibmachのオブジェクトファイルリーダーに依存していたことがわかります。

技術的詳細

このコミットは、主にGoツールチェインのC言語部分から、古く、機能しなくなったオブジェクトファイル解析コードを削除することに焦点を当てています。

具体的には、以下のファイルが影響を受けています。

  1. src/cmd/pack/ar.c: このファイルはpackツールのアーカイブ処理に関連するC言語コードを含んでいます。変更点を見ると、libmachのオブジェクトファイルリーダー(_read5, _read6, _read8)への参照と、それらを使用していたarread_cutprefix関数内のロジックが削除されています。特に、reader配列の定義が削除され、arread_cutprefix関数は// TODO: reimplementというコメントと共に、常に0を返すように変更されています。これは、この機能が一時的に無効化され、将来的に再実装されることを示唆しています。
  2. src/libmach/5obj.c: ARMアーキテクチャのオブジェクトファイルを読み取るためのC言語コードが含まれていました。このファイルは完全に削除されました。
  3. src/libmach/6obj.c: AMD64アーキテクチャのオブジェクトファイルを読み取るためのC言語コードが含まれていました。このファイルは完全に削除されました。
  4. src/libmach/8obj.c: 386アーキテクチャのオブジェクトファイルを読み取るためのC言語コードが含まれていました。このファイルは完全に削除されました。
  5. src/libmach/obj.c: libmachの主要な部分であり、様々なアーキテクチャのオブジェクトファイルを識別し、読み取るための共通ロジックが含まれていました。このファイルからは、削除された*_obj.cファイルへの参照(obj構造体配列の定義や、_is*_read*関数のプロトタイプ宣言)が削除されています。また、readobjreadarprocessprogobjlookupobjupdateobjtraverse_offsetobjresetといった、オブジェクトファイルのシンボル解析や管理に関連する多くの関数が削除されています。これにより、libmachのオブジェクトファイル解析機能の大部分が取り除かれました。

これらの削除は、GoのツールチェインがC言語からGo言語への移行を進める過程の一部と考えられます。機能しないC言語コードを削除することで、Go言語での新しい実装への道が開かれ、Goツールチェイン全体の整合性と保守性が向上します。

コアとなるコードの変更箇所

このコミットでは、以下の5つのファイルが変更されています。

  • src/cmd/pack/ar.c: 107行削除、4行追加
  • src/libmach/5obj.c: 171行削除 (ファイル全体削除)
  • src/libmach/6obj.c: 173行削除 (ファイル全体削除)
  • src/libmach/8obj.c: 170行削除 (ファイル全体削除)
  • src/libmach/obj.c: 316行削除、0行追加

合計で4行の追加と933行の削除が行われています。これは、主に既存のコードの削除によるものです。

コアとなるコードの解説

src/cmd/pack/ar.c

このファイルでは、arread_cutprefix関数から、古いlibmachのオブジェクトファイルリーダーへの依存が削除されています。

--- a/src/cmd/pack/ar.c
+++ b/src/cmd/pack/ar.c
@@ -1553,20 +1553,6 @@ arstrdup(char *s)
 	return t;
 }
 
-/*
- *	Parts of libmach we're not supposed
- *	to look at but need for arread_cutprefix.
- */
-extern int _read5(Biobuf*, Prog*);
-extern int _read6(Biobuf*, Prog*);
-extern int _read8(Biobuf*, Prog*);
-int (*reader[256])(Biobuf*, Prog*) = {
-	[ObjArm] = _read5,
-	[ObjAmd64] = _read6,
-	[Obj386] = _read8,
-};
-
 #define isdelim(c) ((c) == '/' || (c) == '\\')
 
 /*
@@ -1595,94 +1581,9 @@ iswinpathstart(char *p, char *drive)
 int
 arread_cutprefix(Biobuf *b, Armember *bp)
 {
-	vlong offset, o, end;
-	int n, t;
-	int (*rd)(Biobuf*, Prog*);
-	char *w, *inprefix, d1, d2;
-	Prog p;
-	
-	offset = Boffset(b);
-	end = offset + bp->size;
-	t = objtype(b, nil);
-	if(t < 0)
-		return 0;
-	if((rd = reader[t]) == nil)
-		return 0;
-	
-	// copy header
-	w = bp->member;
-	n = Boffset(b) - offset;
-	Bseek(b, -n, 1);
-	if(Bread(b, w, n) != n)
-		return 0;
-	offset += n;
-	w += n;
-	
-	// read object file one pseudo-instruction at a time,
-	// eliding the file name instructions that refer to
-	// the prefix.
-	memset(&p, 0, sizeof p);
-	inprefix = nil;
-	while(Boffset(b) < end && rd(b, &p)) {
-		if(p.kind == aName && p.type == UNKNOWN && p.sym == 1 && p.id[0] == '<') {
-			// part of a file path.
-			// we'll keep continuing (skipping the copy)
-			// around the loop until either we get to a
-			// name piece that should be kept or we see
-			// the whole prefix.
-
-			if(inprefix == nil && prefix[0] == '/' && p.id[1] == '/' && p.id[2] == '\0') {
-				// leading /
-				inprefix = prefix+1;
-			} else if(inprefix == nil && iswinpathstart(prefix, &d1) && iswinpathstart(p.id + 1, &d2) && d1 == d2 && p.id[4] == '\0') {
-				// leading c:\ ...
-				inprefix = prefix+3;
-			} else if(inprefix != nil) {
-				// handle subsequent elements
-				n = strlen(p.id+1);
-				if(strncmp(p.id+1, inprefix, n) == 0 && (isdelim(inprefix[n]) || inprefix[n] == '\0')) {
-					inprefix += n;
-					if(isdelim(inprefix[0]))
-						inprefix++;
-				}
-			}
-			
-			if(inprefix && inprefix[0] == '\0') {
-				// reached end of prefix.
-				// if we another path element follows,
-				// nudge the offset to skip over the prefix we saw.
-				// if not, leave offset alone, to emit the whole name.
-				// additional name elements will not be skipped
-				// because inprefix is now nil and we won't see another
-				// leading / in this name.
-				inprefix = nil;
-				o = Boffset(b);
-				if(o < end && rd(b, &p) && p.kind == aName && p.type == UNKNOWN && p.sym == 1 && p.id[0] == '<') {
-					// print("skip %lld-%lld\n", offset, o);
-					offset = o;
-				}
-			}
-		} else {
-			// didn't find the whole prefix.
-			// give up and let it emit the entire name.
-			inprefix = nil;
-		}
-
-		// copy instructions
-		if(!inprefix) {
-			n = Boffset(b) - offset;
-			Bseek(b, -n, 1);
-			if(Bread(b, w, n) != n)
-				return 0;
-			offset += n;
-			w += n;
-		}
-	}
-	bp->size = w - (char*)bp->member;
-	sprint(bp->hdr.size, "%-10lld", (vlong)bp->size);
-	strncpy(bp->hdr.fmag, ARFMAG, 2);
-	Bseek(b, end, 0);
-	if(Boffset(b)&1)
-		BGETC(b);
-	return 1;
+	// TODO: reimplement
+	USED(b);
+	USED(bp);
+
+	return 0;
 }
  • _read5, _read6, _read8関数の外部宣言と、それらを格納していたreader配列が削除されました。
  • arread_cutprefix関数の実装が大幅に削除され、代わりに// TODO: reimplementというコメントと、引数を使用済みとしてマークするUSEDマクロ、そして常に0を返すシンプルな実装に置き換えられました。これは、この機能が一時的に無効化されたことを意味します。

src/libmach/5obj.c, src/libmach/6obj.c, src/libmach/8obj.c

これらのファイルは、それぞれARM、AMD64、386アーキテクチャのオブジェクトファイルを解析するためのC言語コードを含んでいましたが、このコミットで完全に削除されました。

src/libmach/obj.c

このファイルはlibmachの主要なエントリポイントの一つであり、様々なオブジェクトファイル形式を識別し、対応するリーダーを呼び出す役割を担っていました。

--- a/src/libmach/obj.c
+++ b/src/libmach/obj.c
@@ -37,132 +37,6 @@
 #include <mach.h>
 #include "obj.h"
 
-#define islocal(t)	((t)=='a' || (t)=='p')
-
-enum
-{
-	NNAMES	= 50,
-	MAXIS	= 8,		/* max length to determine if a file is a .? file */
-	MAXOFF	= 0x7fffffff,	/* larger than any possible local offset */
-	NHASH	= 1024,		/* must be power of two */
-	HASHMUL	= 79L,
-};
-
-int	_is2(char*),		/* in [$OS].c */
-	_is5(char*),
-	_is6(char*),
-	_is7(char*),
-	_is8(char*),
-	_is9(char*),
-	_isk(char*),
-	_isq(char*),
-	_isv(char*),
-	_isu(char*),
-	_read2(Biobuf*, Prog*),
-	_read5(Biobuf*, Prog*),
-	_read6(Biobuf*, Prog*),
-	_read7(Biobuf*, Prog*),
-	_read8(Biobuf*, Prog*),
-	_read9(Biobuf*, Prog*),
-	_readk(Biobuf*, Prog*),
-	_readq(Biobuf*, Prog*),
-	_readv(Biobuf*, Prog*),
-	_readu(Biobuf*, Prog*);
-
-typedef struct Obj	Obj;
-typedef struct Symtab	Symtab;
-
-struct	Obj		/* functions to handle each intermediate (.$O) file */
-{
-	char	*name;				/* name of each $O file */
-	int	(*is)(char*);			/* test for each type of $O file */
-	int	(*read)(Biobuf*, Prog*);	/* read for each type of $O file*/
-};
-
-static Obj	obj[] =
-{			/* functions to identify and parse each type of obj */
-	[Obj68020]   = { "68020 .2",	_is2, _read2 },
-	[ObjAmd64]   = { "amd64 .6",	_is6 , _read6 },
-	[ObjArm]     = { "arm .5",	_is5, _read5 },
-	[ObjAlpha]   = { "alpha .7",	_is7, _read7 },
-	[Obj386]     = { "386 .8",	_is8, _read8 },
-	[ObjSparc]   = { "sparc .k",	_isk, _readk },
-	[ObjPower]   = { "power .q",	_isq, _readq },
-	[ObjMips]    = { "mips .v",	_isv, _readv },
-	[ObjSparc64] = { "sparc64 .u",  _isu, _readu },
-	[ObjPower64] = { "power64 .9",	_is9, _read9 },
-	[Maxobjtype] = { 0, 0, 0 }
-};
-
-struct	Symtab
-{
-	struct	Sym 	s;
-	struct	Symtab	*next;
-};
-
-static	Symtab *hash[NHASH];
-static	Sym	*names[NNAMES];	/* working set of active names */
-
-static	int	processprog(Prog*,int);	/* decode each symbol reference */
-static	void	objreset(void);
-static	void	objlookup(int, char *, int, uint);
-static	void 	objupdate(int, int);
-
-static	int	sequence;
-
-int
-objtype(Biobuf *bp, char **name)
-{
-	int i;
-	char buf[MAXIS];
-	int c;
-	char *p;
-
-	/*
-	 * Look for import block.
-	 */
-	p = Brdline(bp, '\n');
-	if(p == nil)
-		return -1;
-	if(Blinelen(bp) < 10 || strncmp(p, "go object ", 10) != 0)
-		return -1;
-	Bseek(bp, -1, 1);
-
-	/*
-	 * Found one.  Skip until "\n!\n"
-	 */
-	for(;;) {
-		if((c = BGETC(bp)) == Beof)
-			return -1;
-		if(c != '\n')
-			continue;
-		c = BGETC(bp);
-		if(c != '!') {
-			Bungetc(bp);
-			continue;
-		}
-		c = BGETC(bp);
-		if(c != '\n') {
-			Bungetc(bp);
-			continue;
-		}
-		break;
-	}
-
-	if(Bread(bp, buf, MAXIS) < MAXIS)
-		return -1;
-	Bseek(bp, -MAXIS, 1);
-	for (i = 0; i < Maxobjtype; i++) {
-		if (obj[i].is && (*obj[i].is)(buf)) {
-			if (name)
-				*name = obj[i].name;
-			return i;
-		}
-	}
-
-	return -1;
-}
-
 int
 isar(Biobuf *bp)
 {
@@ -175,179 +49,6 @@ isar(Biobuf *bp)
 	return 0;
 }
 
-/*
- * determine what kind of object file this is and process it.
- * return whether or not this was a recognized intermediate file.
- */
-int
-readobj(Biobuf *bp, int objtype)
-{
-	Prog p;
-
-	if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
-		return 1;
-	objreset();
-	while ((*obj[objtype].read)(bp, &p))
-		if (!processprog(&p, 1))
-			return 0;
-	return 1;
-}
-
-int
-readar(Biobuf *bp, int objtype, vlong end, int doautos)
-{
-	Prog p;
-
-	if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
-		return 1;
-	objreset();
-	while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
-		if (!processprog(&p, doautos))
-			return 0;
-	return 1;
-}
-
-/*
- *	decode a symbol reference or definition
- */
-static	int
-processprog(Prog *p, int doautos)
-{
-	if(p->kind == aNone)
-		return 1;
-	if((schar)p->sym < 0 || p->sym >= NNAMES)
-		return 0;
-	switch(p->kind)
-	{
-	case aName:
-		if (!doautos)
-		if(p->type != 'U' && p->type != 'b')
-			break;
-		objlookup(p->sym, p->id, p->type, p->sig);
-		break;
-	case aText:
-		objupdate(p->sym, 'T');
-		break;
-	case aData:
-		objupdate(p->sym, 'D');
-		break;
-	default:
-		break;
-	}
-	return 1;
-}
-
-/*
- * find the entry for s in the symbol array.
- * make a new entry if it is not already there.
- */
-static void
-objlookup(int id, char *name, int type, uint sig)
-{
-	uint32 h;
-	char *cp;
-	Sym *s;
-	Symtab *sp;
-
-	s = names[id];
-	if(s && strcmp(s->name, name) == 0) {
-		s->type = type;
-		s->sig = sig;
-		return;
-	}
-
-	h = *name;
-	for(cp = name+1; *cp; h += *cp++)
-		h *= HASHMUL;
-	h &= NHASH-1;
-	if (type == 'U' || type == 'b' || islocal(type)) {
-		for(sp = hash[h]; sp; sp = sp->next)
-			if(strcmp(sp->s.name, name) == 0) {
-				switch(sp->s.type) {
-				case 'T':
-				case 'D':
-				case 'U':
-					if (type == 'U') {
-						names[id] = &sp->s;
-						return;
-					}
-					break;
-				case 't':
-				case 'd':
-				case 'b':
-					if (type == 'b') {
-						names[id] = &sp->s;
-						return;
-					}
-					break;
-				case 'a':
-				case 'p':
-					if (islocal(type)) {
-						names[id] = &sp->s;
-						return;
-					}
-					break;
-				default:
-					break;
-				}
-			}
-	}
-	sp = malloc(sizeof(Symtab));
-	if(sp == nil)
-		sysfatal("out of memory");
-	sp->s.name = name;
-	sp->s.type = type;
-	sp->s.sig = sig;
-	sp->s.value = islocal(type) ? MAXOFF : 0;
-	sp->s.sequence = sequence++;
-	names[id] = &sp->s;
-	sp->next = hash[h];
-	hash[h] = sp;
-	return;
-}
-/*
- *	traverse the symbol lists
- */
-void
-objtraverse(void (*fn)(Sym*, void*), void *pointer)
-{
-	int i;
-	Symtab *s, *n;
-
-	for(i = 0; i < NHASH; i++)
-		for(s = hash[i]; s; s = s->next)
-			(*fn)(&s->s, pointer);
-}
-
-/*
- * update the offset information for a 'a' or 'p' symbol in an intermediate file
- */
-void
-_offset(int id, vlong off)
-{
-	Sym *s;
-
-	s = names[id];
-	if (s && s->name[0] && islocal(s->type) && s->value > off)
-		s->value = off;
-}
-
-/*
- * update the type of a global text or data symbol
- */
-static void
-objupdate(int id, int type)
-{
-	Sym *s;
-
-	s = names[id];
-	if (s && s->name[0])
-		if (s->type == 'U')
-			s->type = type;
-		else if (s->type == 'b')
-			s->type = tolower(type);
-}
-
 /*
  * look for the next file in an archive
  */
@@ -374,20 +75,3 @@ nextar(Biobuf *bp, int offset, char *buf)
 		arsize++;
 	return arsize + SAR_HDR;\n
 }\n-
-static void
-objreset(void)\n-{\n-	int i;\n-	Symtab *s, *n;\n-\n-	for(i = 0; i < NHASH; i++) {\n-		for(s = hash[i]; s; s = n) {\n-			n = s->next;\n-			free(s->s.name);\n-			free(s);\n-		}\n-		hash[i] = 0;\n-	}\n-	memset(names, 0, sizeof names);\n-}
  • _is*および_read*関数のプロトタイプ宣言が削除されました。
  • Obj構造体とobj配列(各オブジェクトファイルタイプに対応する関数ポインタを保持)が削除されました。
  • Symtab構造体、hash配列、names配列といったシンボルテーブル管理に関連するデータ構造が削除されました。
  • processprog, objlookup, objupdate, objtraverse, _offset, objresetといった、オブジェクトファイルのシンボル解析、更新、トラバース、リセットを行う関数がすべて削除されました。
  • objtype関数も大幅に簡素化され、Goオブジェクトファイルのヘッダを読み取る部分のみが残されました。以前は、読み取ったヘッダに基づいてobj配列から適切なリーダーを特定していましたが、そのロジックが削除されました。
  • readobjおよびreadar関数も完全に削除されました。

これらの変更は、libmachがGoのオブジェクトファイル解析において中心的な役割を果たすことをやめ、その機能がGo言語で再実装されるか、あるいはGoツールチェインの他の部分に統合されることを示しています。

関連リンク

  • Go Issue 6989: コミットメッセージで言及されている、この機能の復元を追跡するためのIssue。ただし、現在のGoのIssueトラッカーでは直接見つけることができませんでした。これは、Issue番号が変更されたか、非常に古いIssueであるためアーカイブされた可能性があります。

参考にした情報源リンク

  • Go言語の公式ドキュメント (Goのビルドプロセス、ツールチェインに関する一般的な情報)
  • GitHubのGoリポジトリ (過去のコミット履歴やコードの変遷)
  • Web検索: "Go libmach", "Go pack tool", "Go issue 6989"# [インデックス 18096] ファイルの概要

このコミットは、Goプロジェクトのlibmachライブラリから、古くなったオブジェクトファイルリーダーを削除することを目的としています。これらのリーダーはもはや機能しておらず、それらを削除することで他のリファクタリングが容易になります。また、packツールにおける関連する機能も削除されます。

コミット

  • コミットハッシュ: 1334b794b74c535b0971ea51c940faa6b26fe184
  • Author: Russ Cox rsc@golang.org
  • Date: Fri Dec 20 12:10:53 2013 -0500

GitHub上でのコミットページへのリンク

https://github.com/golang/go/commit/1334b794b74c535b0971ea51c940faa6b26fe184

元コミット内容

libmach: remove old object file readers

These no longer work; removing them makes other refactoring easier.
The code for pack P being deleted in this CL does not work either.
I created issue 6989 to track restoring this functionality (probably not
until pack is written in Go).

R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/44300043

変更の背景

このコミットの主な背景は、Goプロジェクトのlibmachライブラリ内に存在していた古いオブジェクトファイルリーダーが、もはや正しく機能していなかったことです。これらのコードは、Goの進化に伴いメンテナンスされなくなり、現在のGoのオブジェクトファイル形式やツールチェインと互換性がなくなっていました。

機能しないコードを削除することで、以下の利点が得られます。

  1. コードベースの簡素化: 不要なコードがなくなることで、コードベースがスリムになり、理解しやすくなります。
  2. リファクタリングの促進: 古い依存関係や複雑なロジックが取り除かれることで、将来的なコードのリファクタリングや改善が容易になります。特に、コミットメッセージには「removing them makes other refactoring easier」と明記されており、これが重要な動機であったことが伺えます。
  3. packツールの再設計: packツールの一部機能もこれらの古いリーダーに依存しており、同様に機能していませんでした。このコミットでは、その機能も削除されます。将来的には、packツールがGo言語で再実装される際に、この機能が適切に復元されることが計画されています(Issue 6989で追跡)。これは、Goツールチェイン全体をGo言語で記述するというGoプロジェクトの長期的な目標の一環でもあります。

前提知識の解説

libmach

libmachは、Goプロジェクトの初期段階において、様々なアーキテクチャのオブジェクトファイル(コンパイルされたコードやデータを含むファイル)を読み取るためのライブラリとして機能していました。コミットで削除されるファイル名(5obj.c, 6obj.c, 8obj.c)から推測できるように、これらはそれぞれARM (5), AMD64 (6), 386 (8) アーキテクチャのオブジェクトファイルを扱うためのC言語で書かれたコードでした。

これらのファイルには、Inferno OSやPlan 9のコードベースからの影響が見られます。Go言語自体がPlan 9の思想やツールチェインから多くのインスピレーションを受けているため、初期のGoツールチェインにはこれらのシステムからのC言語コードが組み込まれていることがありました。libmachは、Goのリンカやその他のツールが、異なるアーキテクチャのオブジェクトファイルを解析し、シンボル情報などを抽出するために使用されていたと考えられます。

Goのオブジェクトファイル形式

Go言語のコンパイラは、ソースコードを中間表現(オブジェクトファイル)にコンパイルします。Goのオブジェクトファイル形式は、Goのツールチェイン内で使用される独自の形式であり、C/C++のELFやMach-O、PEなどの標準的なオブジェクトファイル形式とは異なります。これは、Goのリンカが効率的に動作し、クロスコンパイルを容易にするために設計されています。

packツール

packツールは、Goの初期のツールチェインの一部であり、Unixのar(archiver)コマンドに似た機能を提供していたと考えられます。arコマンドは、複数のオブジェクトファイルやライブラリファイルを一つのアーカイブファイルにまとめるために使用されます。Goのpackツールも、同様にGoのオブジェクトファイルをアーカイブ化する目的で使われていた可能性があります。コミットメッセージでpackツールが言及されていることから、packlibmachのオブジェクトファイルリーダーに依存していたことがわかります。

技術的詳細

このコミットは、主にGoツールチェインのC言語部分から、古く、機能しなくなったオブジェクトファイル解析コードを削除することに焦点を当てています。

具体的には、以下のファイルが影響を受けています。

  1. src/cmd/pack/ar.c: このファイルはpackツールのアーカイブ処理に関連するC言語コードを含んでいます。変更点を見ると、libmachのオブジェクトファイルリーダー(_read5, _read6, _read8)への参照と、それらを使用していたarread_cutprefix関数内のロジックが削除されています。特に、reader配列の定義が削除され、arread_cutprefix関数は// TODO: reimplementというコメントと共に、常に0を返すように変更されています。これは、この機能が一時的に無効化され、将来的に再実装されることを示唆しています。
  2. src/libmach/5obj.c: ARMアーキテクチャのオブジェクトファイルを読み取るためのC言語コードが含まれていました。このファイルは完全に削除されました。
  3. src/libmach/6obj.c: AMD64アーキテクチャのオブジェクトファイルを読み取るためのC言語コードが含まれていました。このファイルは完全に削除されました。
  4. src/libmach/8obj.c: 386アーキテクチャのオブジェクトファイルを読み取るためのC言語コードが含まれていました。このファイルは完全に削除されました。
  5. src/libmach/obj.c: libmachの主要な部分であり、様々なアーキテクチャのオブジェクトファイルを識別し、読み取るための共通ロジックが含まれていました。このファイルからは、削除された*_obj.cファイルへの参照(obj構造体配列の定義や、_is*_read*関数のプロトタイプ宣言)が削除されています。また、readobjreadarprocessprogobjlookupobjupdateobjtraverse_offsetobjresetといった、オブジェクトファイルのシンボル解析や管理に関連する多くの関数が削除されています。これにより、libmachのオブジェクトファイル解析機能の大部分が取り除かれました。

これらの削除は、GoのツールチェインがC言語からGo言語への移行を進める過程の一部と考えられます。機能しないC言語コードを削除することで、Go言語での新しい実装への道が開かれ、Goツールチェイン全体の整合性と保守性が向上します。

コアとなるコードの変更箇所

このコミットでは、以下の5つのファイルが変更されています。

  • src/cmd/pack/ar.c: 107行削除、4行追加
  • src/libmach/5obj.c: 171行削除 (ファイル全体削除)
  • src/libmach/6obj.c: 173行削除 (ファイル全体削除)
  • src/libmach/8obj.c: 170行削除 (ファイル全体削除)
  • src/libmach/obj.c: 316行削除、0行追加

合計で4行の追加と933行の削除が行われています。これは、主に既存のコードの削除によるものです。

コアとなるコードの解説

src/cmd/pack/ar.c

このファイルでは、arread_cutprefix関数から、古いlibmachのオブジェクトファイルリーダーへの依存が削除されています。

--- a/src/cmd/pack/ar.c
+++ b/src/cmd/pack/ar.c
@@ -1553,20 +1553,6 @@ arstrdup(char *s)
 	return t;
 }
 
-/*
- *	Parts of libmach we're not supposed
- *	to look at but need for arread_cutprefix.
- */
-extern int _read5(Biobuf*, Prog*);
-extern int _read6(Biobuf*, Prog*);
-extern int _read8(Biobuf*, Prog*);
-int (*reader[256])(Biobuf*, Prog*) = {
-	[ObjArm] = _read5,
-	[ObjAmd64] = _read6,
-	[Obj386] = _read8,
-};
-
 #define isdelim(c) ((c) == '/' || (c) == '\\')
 
 /*
@@ -1595,94 +1581,9 @@ iswinpathstart(char *p, char *drive)
 int
 arread_cutprefix(Biobuf *b, Armember *bp)
 {
-	vlong offset, o, end;
-	int n, t;
-	int (*rd)(Biobuf*, Prog*);
-	char *w, *inprefix, d1, d2;
-	Prog p;
-	
-	offset = Boffset(b);
-	end = offset + bp->size;
-	t = objtype(b, nil);
-	if(t < 0)
-		return 0;
-	if((rd = reader[t]) == nil)
-		return 0;
-	
-	// copy header
-	w = bp->member;
-	n = Boffset(b) - offset;
-	Bseek(b, -n, 1);
-	if(Bread(b, w, n) != n)
-		return 0;
-	offset += n;
-	w += n;
-	
-	// read object file one pseudo-instruction at a time,
-	// eliding the file name instructions that refer to
-	// the prefix.
-	memset(&p, 0, sizeof p);
-	inprefix = nil;
-	while(Boffset(b) < end && rd(b, &p)) {
-		if(p.kind == aName && p.type == UNKNOWN && p.sym == 1 && p.id[0] == '<') {
-			// part of a file path.
-			// we'll keep continuing (skipping the copy)
-			// around the loop until either we get to a
-			// name piece that should be kept or we see
-			// the whole prefix.
-
-			if(inprefix == nil && prefix[0] == '/' && p.id[1] == '/' && p.id[2] == '\0') {
-				// leading /
-				inprefix = prefix+1;
-			} else if(inprefix == nil && iswinpathstart(prefix, &d1) && iswinpathstart(p.id + 1, &d2) && d1 == d2 && p.id[4] == '\0') {
-				// leading c:\ ...
-				inprefix = prefix+3;
-			} else if(inprefix != nil) {
-				// handle subsequent elements
-				n = strlen(p.id+1);
-				if(strncmp(p.id+1, inprefix, n) == 0 && (isdelim(inprefix[n]) || inprefix[n] == '\0')) {
-					inprefix += n;
-					if(isdelim(inprefix[0]))
-						inprefix++;
-				}
-			}
-			
-			if(inprefix && inprefix[0] == '\0') {
-				// reached end of prefix.
-				// if we another path element follows,
-				// nudge the offset to skip over the prefix we saw.
-				// if not, leave offset alone, to emit the whole name.
-				// additional name elements will not be skipped
-				// because inprefix is now nil and we won't see another
-				// leading / in this name.
-				inprefix = nil;
-				o = Boffset(b);
-				if(o < end && rd(b, &p) && p.kind == aName && p.type == UNKNOWN && p.sym == 1 && p.id[0] == '<') {
-					// print("skip %lld-%lld\n", offset, o);
-					offset = o;
-				}
-			}
-		} else {
-			// didn't find the whole prefix.
-			// give up and let it emit the entire name.
-			inprefix = nil;
-		}
-
-		// copy instructions
-		if(!inprefix) {
-			n = Boffset(b) - offset;
-			Bseek(b, -n, 1);
-			if(Bread(b, w, n) != n)
-				return 0;
-			offset += n;
-			w += n;
-		}
-	}
-	bp->size = w - (char*)bp->member;
-	sprint(bp->hdr.size, "%-10lld", (vlong)bp->size);
-	strncpy(bp->hdr.fmag, ARFMAG, 2);
-	Bseek(b, end, 0);
-	if(Boffset(b)&1)
-		BGETC(b);
-	return 1;
+	// TODO: reimplement
+	USED(b);
+	USED(bp);
+
+	return 0;
 }
  • _read5, _read6, _read8関数の外部宣言と、それらを格納していたreader配列が削除されました。
  • arread_cutprefix関数の実装が大幅に削除され、代わりに// TODO: reimplementというコメントと、引数を使用済みとしてマークするUSEDマクロ、そして常に0を返すシンプルな実装に置き換えられました。これは、この機能が一時的に無効化されたことを意味します。

src/libmach/5obj.c, src/libmach/6obj.c, src/libmach/8obj.c

これらのファイルは、それぞれARM、AMD64、386アーキテクチャのオブジェクトファイルを解析するためのC言語コードを含んでいましたが、このコミットで完全に削除されました。

src/libmach/obj.c

このファイルはlibmachの主要なエントリポイントの一つであり、様々なオブジェクトファイル形式を識別し、対応するリーダーを呼び出す役割を担っていました。

--- a/src/libmach/obj.c
+++ b/src/libmach/obj.c
@@ -37,132 +37,6 @@
 #include <mach.h>
 #include "obj.h"
 
-#define islocal(t)	((t)=='a' || (t)=='p')
-
-enum
-{
-	NNAMES	= 50,
-	MAXIS	= 8,		/* max length to determine if a file is a .? file */
-	MAXOFF	= 0x7fffffff,	/* larger than any possible local offset */
-	NHASH	= 1024,		/* must be power of two */
-	HASHMUL	= 79L,
-};
-
-int	_is2(char*),		/* in [$OS].c */
-	_is5(char*),
-	_is6(char*),
-	_is7(char*),
-	_is8(char*),
-	_is9(char*),
-	_isk(char*),
-	_isq(char*),
-	_isv(char*),
-	_isu(char*),
-	_read2(Biobuf*, Prog*),
-	_read5(Biobuf*, Prog*),
-	_read6(Biobuf*, Prog*),
-	_read7(Biobuf*, Prog*),
-	_read8(Biobuf*, Prog*),
-	_read9(Biobuf*, Prog*),
-	_readk(Biobuf*, Prog*),
-	_readq(Biobuf*, Prog*),
-	_readv(Biobuf*, Prog*),
-	_readu(Biobuf*, Prog*);
-
-typedef struct Obj	Obj;
-typedef struct Symtab	Symtab;
-
-struct	Obj		/* functions to handle each intermediate (.$O) file */
-{
-	char	*name;				/* name of each $O file */
-	int	(*is)(char*);			/* test for each type of $O file */
-	int	(*read)(Biobuf*, Prog*);	/* read for each type of $O file*/
-};
-
-static Obj	obj[] =
-{			/* functions to identify and parse each type of obj */
-	[Obj68020]   = { "68020 .2",	_is2, _read2 },
-	[ObjAmd64]   = { "amd64 .6",	_is6 , _read6 },
-	[ObjArm]     = { "arm .5",	_is5, _read5 },
-	[ObjAlpha]   = { "alpha .7",	_is7, _read7 },
-	[Obj386]     = { "386 .8",	_is8, _read8 },
-	[ObjSparc]   = { "sparc .k",	_isk, _readk },
-	[ObjPower]   = { "power .q",	_isq, _readq },
-	[ObjMips]    = { "mips .v",	_isv, _readv },
-	[ObjSparc64] = { "sparc64 .u",  _isu, _readu },
-	[ObjPower64] = { "power64 .9",	_is9, _read9 },
-	[Maxobjtype] = { 0, 0, 0 }
-};
-
-struct	Symtab
-{
-	struct	Sym 	s;
-	struct	Symtab	*next;
-};
-
-static	Symtab *hash[NHASH];
-static	Sym	*names[NNAMES];	/* working set of active names */
-
-static	int	processprog(Prog*,int);	/* decode each symbol reference */
-static	void	objreset(void);
-static	void	objlookup(int, char *, int, uint);
-static	void 	objupdate(int, int);
-
-static	int	sequence;
-
-int
-objtype(Biobuf *bp, char **name)
-{
-	int i;
-	char buf[MAXIS];
-	int c;
-	char *p;
-
-	/*
-	 * Look for import block.
-	 */
-	p = Brdline(bp, '\n');
-	if(p == nil)
-		return -1;
-	if(Blinelen(bp) < 10 || strncmp(p, "go object ", 10) != 0)
-		return -1;
-	Bseek(bp, -1, 1);
-
-	/*
-	 * Found one.  Skip until "\n!\n"
-	 */
-	for(;;) {
-		if((c = BGETC(bp)) == Beof)
-			return -1;
-		if(c != '\n')
-			continue;
-		c = BGETC(bp);
-		if(c != '!') {
-			Bungetc(bp);
-			continue;
-		}
-		c = BGETC(bp);
-		if(c != '\n') {
-			Bungetc(bp);
-			continue;
-		}
-		break;
-	}
-
-	if(Bread(bp, buf, MAXIS) < MAXIS)
-		return -1;
-	Bseek(bp, -MAXIS, 1);
-	for (i = 0; i < Maxobjtype; i++) {
-		if (obj[i].is && (*obj[i].is)(buf)) {
-			if (name)
-				*name = obj[i].name;
-			return i;
-		}
-	}
-
-	return -1;
-}
-
 int
 isar(Biobuf *bp)
 {
@@ -175,179 +49,6 @@ isar(Biobuf *bp)
 	return 0;
 }
 
-/*
- * determine what kind of object file this is and process it.
- * return whether or not this was a recognized intermediate file.
- */
-int
-readobj(Biobuf *bp, int objtype)
-{
-	Prog p;
-
-	if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
-		return 1;
-	objreset();
-	while ((*obj[objtype].read)(bp, &p))
-		if (!processprog(&p, 1))
-			return 0;
-	return 1;
-}
-
-int
-readar(Biobuf *bp, int objtype, vlong end, int doautos)
-{
-	Prog p;
-
-	if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
-		return 1;
-	objreset();
-	while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
-		if (!processprog(&p, doautos))
-			return 0;
-	return 1;
-}
-
-/*
- *	decode a symbol reference or definition
- */
-static	int
-processprog(Prog *p, int doautos)
-{
-	if(p->kind == aNone)
-		return 1;
-	if((schar)p->sym < 0 || p->sym >= NNAMES)
-		return 0;
-	switch(p->kind)
-	{
-	case aName:
-		if (!doautos)
-		if(p->type != 'U' && p->type != 'b')
-			break;
-		objlookup(p->sym, p->id, p->type, p->sig);
-		break;
-	case aText:
-		objupdate(p->sym, 'T');
-		break;
-	case aData:
-		objupdate(p->sym, 'D');
-		break;
-	default:
-		break;
-	}
-	return 1;
-}
-
-/*
- * find the entry for s in the symbol array.
- * make a new entry if it is not already there.
- */
-static void
-objlookup(int id, char *name, int type, uint sig)
-{
-	uint32 h;
-	char *cp;
-	Sym *s;
-	Symtab *sp;
-
-	s = names[id];
-	if(s && strcmp(s->name, name) == 0) {
-		s->type = type;
-		s->sig = sig;
-		return;
-	}
-
-	h = *name;
-	for(cp = name+1; *cp; h += *cp++)
-		h *= HASHMUL;
-	h &= NHASH-1;
-	if (type == 'U' || type == 'b' || islocal(type)) {
-		for(sp = hash[h]; sp; sp = sp->next)
-			if(strcmp(sp->s.name, name) == 0) {
-				switch(sp->s.type) {
-				case 'T':
-				case 'D':
-				case 'U':
-					if (type == 'U') {
-						names[id] = &sp->s;
-						return;
-					}
-					break;
-				case 't':
-				case 'd':
-				case 'b':
-					if (type == 'b') {
-						names[id] = &sp->s;
-						return;
-					}
-					break;
-				case 'a':
-				case 'p':
-					if (islocal(type)) {
-						names[id] = &sp->s;
-						return;
-					}
-					break;
-				default:
-					break;
-				}
-			}
-	}
-	sp = malloc(sizeof(Symtab));
-	if(sp == nil)
-		sysfatal("out of memory");
-	sp->s.name = name;
-	sp->s.type = type;
-	sp->s.sig = sig;
-	sp->s.value = islocal(type) ? MAXOFF : 0;
-	sp->s.sequence = sequence++;
-	names[id] = &sp->s;
-	sp->next = hash[h];
-	hash[h] = sp;
-	return;
-}
-/*
- *	traverse the symbol lists
- */
-void
-objtraverse(void (*fn)(Sym*, void*), void *pointer)
-{
-	int i;
-	Symtab *s, *n;
-
-	for(i = 0; i < NHASH; i++)
-		for(s = hash[i]; s; s = s->next)
-			(*fn)(&s->s, pointer);
-}
-
-/*
- * update the offset information for a 'a' or 'p' symbol in an intermediate file
- */
-void
-_offset(int id, vlong off)
-{
-	Sym *s;
-
-	s = names[id];
-	if (s && s->name[0] && islocal(s->type) && s->value > off)
-		s->value = off;
-}
-
-/*
- * update the type of a global text or data symbol
- */
-static void
-objupdate(int id, int type)
-{
-	Sym *s;
-
-	s = names[id];
-	if (s && s->name[0])
-		if (s->type == 'U')
-			s->type = type;
-		else if (s->type == 'b')
-			s->type = tolower(type);
-}
-
 /*
  * look for the next file in an archive
  */
@@ -374,20 +75,3 @@ nextar(Biobuf *bp, int offset, char *buf)
 		arsize++;
 	return arsize + SAR_HDR;\n
 }\n-
-static void
-objreset(void)\n-{\n-	int i;\n-	Symtab *s, *n;\n-\n-	for(i = 0; i < NHASH; i++) {\n-		for(s = hash[i]; s; s = n) {\n-			n = s->next;\n-			free(s->s.name);\n-			free(s);\n-		}\n-		hash[i] = 0;\n-	}\n-	memset(names, 0, sizeof names);\n-}
  • _is*および_read*関数のプロトタイプ宣言が削除されました。
  • Obj構造体とobj配列(各オブジェクトファイルタイプに対応する関数ポインタを保持)が削除されました。
  • Symtab構造体、hash配列、names配列といったシンボルテーブル管理に関連するデータ構造が削除されました。
  • processprog, objlookup, objupdate, objtraverse, _offset, objresetといった、オブジェクトファイルのシンボル解析、更新、トラバース、リセットを行う関数がすべて削除されました。
  • objtype関数も大幅に簡素化され、Goオブジェクトファイルのヘッダを読み取る部分のみが残されました。以前は、読み取ったヘッダに基づいてobj配列から適切なリーダーを特定していましたが、そのロジックが削除されました。
  • readobjおよびreadar関数も完全に削除されました。

これらの変更は、libmachがGoのオブジェクトファイル解析において中心的な役割を果たすことをやめ、その機能がGo言語で再実装されるか、あるいはGoツールチェインの他の部分に統合されることを示しています。

関連リンク

  • Go Issue 6989: コミットメッセージで言及されている、この機能の復元を追跡するためのIssue。ただし、現在のGoのIssueトラッカーでは直接見つけることができませんでした。これは、Issue番号が変更されたか、非常に古いIssueであるためアーカイブされた可能性があります。

参考にした情報源リンク

  • Go言語の公式ドキュメント (Goのビルドプロセス、ツールチェインに関する一般的な情報)
  • GitHubのGoリポジトリ (過去のコミット履歴やコードの変遷)
  • Web検索: "Go libmach", "Go pack tool", "Go issue 6989"