|
| 1 | +package patchpkg |
| 2 | + |
| 3 | +import ( |
| 4 | +"debug/elf" |
| 5 | +"errors" |
| 6 | +"fmt" |
| 7 | +"io" |
| 8 | +"iter" |
| 9 | +"log/slog" |
| 10 | +"os" |
| 11 | +"path/filepath" |
| 12 | +"strings" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | +// SystemLibSearchPaths match the system library paths for common Linux |
| 17 | +// distributions. |
| 18 | +SystemLibSearchPaths = []string{ |
| 19 | +"/lib*/*-linux-gnu", // Debian |
| 20 | +"/lib*", // Red Hat |
| 21 | +"/var/lib*/*/lib*", // Docker |
| 22 | +} |
| 23 | + |
| 24 | +// EnvLibrarySearchPath matches the paths in the LIBRARY_PATH |
| 25 | +// environment variable. |
| 26 | +EnvLibrarySearchPath = filepath.SplitList(os.Getenv("LIBRARY_PATH")) |
| 27 | + |
| 28 | +// EnvLDLibrarySearchPath matches the paths in the LD_LIBRARY_PATH |
| 29 | +// environment variable. |
| 30 | +EnvLDLibrarySearchPath = filepath.SplitList(os.Getenv("LD_LIBRARY_PATH")) |
| 31 | + |
| 32 | +// CUDALibSearchPaths match the common installation directories for CUDA |
| 33 | +// libraries. |
| 34 | +CUDALibSearchPaths = []string{ |
| 35 | +// Common non-package manager locations. |
| 36 | +"/opt/cuda/lib*", |
| 37 | +"/opt/nvidia/lib*", |
| 38 | +"/usr/local/cuda/lib*", |
| 39 | +"/usr/local/nvidia/lib*", |
| 40 | + |
| 41 | +// Unlikely, but might as well try. |
| 42 | +"/lib*/nvidia", |
| 43 | +"/lib*/cuda", |
| 44 | +"/usr/lib*/nvidia", |
| 45 | +"/usr/lib*/cuda", |
| 46 | +"/usr/local/lib*", |
| 47 | +"/usr/local/lib*/nvidia", |
| 48 | +"/usr/local/lib*/cuda", |
| 49 | +} |
| 50 | +) |
| 51 | + |
| 52 | +// SharedLibrary describes an ELF shared library (object). |
| 53 | +// |
| 54 | +// Note that the various name fields document the common naming and versioning |
| 55 | +// conventions, but it is possible for a library to deviate from them. |
| 56 | +// |
| 57 | +// For an introduction to Linux shared libraries, see |
| 58 | +// https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html |
| 59 | +type SharedLibrary struct { |
| 60 | +*os.File |
| 61 | + |
| 62 | +// LinkerName is the soname without any version suffix (libfoo.so). It |
| 63 | +// is typically a symlink pointing to Soname. The build-time linker |
| 64 | +// looks for this name by default. |
| 65 | +LinkerName string |
| 66 | + |
| 67 | +// Soname is the shared object name from the library's DT_SONAME field. |
| 68 | +// It usually includes a version number suffix (libfoo.so.1). Other ELF |
| 69 | +// binaries that depend on this library typically specify this name in |
| 70 | +// the DT_NEEDED field. |
| 71 | +Soname string |
| 72 | + |
| 73 | +// RealName is the absolute path to the file that actually contains the |
| 74 | +// library code. It is typically the soname plus a minor version and |
| 75 | +// release number (libfoo.so.1.0.0). |
| 76 | +RealName string |
| 77 | +} |
| 78 | + |
| 79 | +// OpenSharedLibrary opens a shared library file. Unlike with ld, name must be |
| 80 | +// an exact path. To search for a library in the usual locations, use |
| 81 | +// [FindSharedLibrary] instead. |
| 82 | +func OpenSharedLibrary(name string) (SharedLibrary, error) { |
| 83 | +lib := SharedLibrary{} |
| 84 | +var err error |
| 85 | +lib.File, err = os.Open(name) |
| 86 | +if err != nil { |
| 87 | +return lib, err |
| 88 | +} |
| 89 | + |
| 90 | +dir, file := filepath.Split(name) |
| 91 | +i := strings.Index(file, ".so") |
| 92 | +if i != -1 { |
| 93 | +lib.LinkerName = dir + file[:i+3] |
| 94 | +} |
| 95 | + |
| 96 | +elfFile, err := elf.NewFile(lib) |
| 97 | +if err == nil { |
| 98 | +soname, _ := elfFile.DynString(elf.DT_SONAME) |
| 99 | +if len(soname) != 0 { |
| 100 | +lib.Soname = soname[0] |
| 101 | +} |
| 102 | +} |
| 103 | + |
| 104 | +real, err := filepath.EvalSymlinks(name) |
| 105 | +if err == nil { |
| 106 | +lib.RealName, _ = filepath.Abs(real) |
| 107 | +} |
| 108 | +return lib, nil |
| 109 | +} |
| 110 | + |
| 111 | +// FindSharedLibrary searches the directories in searchPath for a shared |
| 112 | +// library. It yields any libraries in the search path directories that have |
| 113 | +// name as a prefix. For example, "libcuda.so" will match "libcuda.so", |
| 114 | +// "libcuda.so.1", and "libcuda.so.550.107.02". The underlying file is only |
| 115 | +// valid for a single iteration, after which it is closed. |
| 116 | +// |
| 117 | +// The search path may contain [filepath.Glob] patterns. See |
| 118 | +// [SystemLibSearchPaths] for some predefined search paths. If name is an |
| 119 | +// absolute path, then FindSharedLibrary opens it directly and doesn't perform |
| 120 | +// any searching. |
| 121 | +func FindSharedLibrary(name string, searchPath ...string) iter.Seq[SharedLibrary] { |
| 122 | +return func(yield func(SharedLibrary) bool) { |
| 123 | +if filepath.IsAbs(name) { |
| 124 | +lib, err := OpenSharedLibrary(name) |
| 125 | +if err == nil { |
| 126 | +yield(lib) |
| 127 | +} |
| 128 | +return |
| 129 | +} |
| 130 | + |
| 131 | +if libPath := os.Getenv("LD_LIBRARY_PATH"); libPath != "" { |
| 132 | +searchPath = append(searchPath, filepath.SplitList(os.Getenv("LD_LIBRARY_PATH"))...) |
| 133 | +} |
| 134 | +if libPath := os.Getenv("LIBRARY_PATH"); libPath != "" { |
| 135 | +searchPath = append(searchPath, filepath.SplitList(libPath)...) |
| 136 | +} |
| 137 | +searchPath = append(searchPath, |
| 138 | +"/lib*/*-linux-gnu", // Debian |
| 139 | +"/lib*", // Red Hat |
| 140 | +) |
| 141 | + |
| 142 | +suffix := globEscape(name) + "*" |
| 143 | +patterns := make([]string, len(searchPath)) |
| 144 | +for i := range searchPath { |
| 145 | +patterns[i] = filepath.Join(searchPath[i], suffix) |
| 146 | +} |
| 147 | +for match := range searchGlobs(patterns) { |
| 148 | +lib, err := OpenSharedLibrary(match) |
| 149 | +if err != nil { |
| 150 | +continue |
| 151 | +} |
| 152 | +ok := yield(lib) |
| 153 | +_ = lib.Close() |
| 154 | +if !ok { |
| 155 | +return |
| 156 | +} |
| 157 | +} |
| 158 | +} |
| 159 | +} |
| 160 | + |
| 161 | +// CopyAndLink copies the shared library to dir and creates the LinkerName and |
| 162 | +// Soname symlinks for it. It creates dir if it doesn't already exist. |
| 163 | +func (lib SharedLibrary) CopyAndLink(dir string) error { |
| 164 | +err := os.MkdirAll(dir, 0o755) |
| 165 | +if err != nil { |
| 166 | +return err |
| 167 | +} |
| 168 | + |
| 169 | +dstPath := filepath.Join(dir, filepath.Base(lib.RealName)) |
| 170 | +dst, err := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0o666) |
| 171 | +if err != nil { |
| 172 | +return err |
| 173 | +} |
| 174 | +defer dst.Close() |
| 175 | + |
| 176 | +_, err = io.Copy(dst, lib) |
| 177 | +if err != nil { |
| 178 | +return err |
| 179 | +} |
| 180 | +err = dst.Close() |
| 181 | +if err != nil { |
| 182 | +return err |
| 183 | +} |
| 184 | + |
| 185 | +sonameLink := filepath.Join(dir, lib.Soname) |
| 186 | +var sonameErr error |
| 187 | +if lib.Soname != "" { |
| 188 | +// Symlink must be relative. |
| 189 | +sonameErr = os.Symlink(filepath.Base(lib.RealName), sonameLink) |
| 190 | +} |
| 191 | + |
| 192 | +linkerNameLink := filepath.Join(dir, lib.LinkerName) |
| 193 | +var linkerNameErr error |
| 194 | +if lib.LinkerName != "" { |
| 195 | +// Symlink must be relative. |
| 196 | +if sonameErr == nil { |
| 197 | +linkerNameErr = os.Symlink(filepath.Base(sonameLink), linkerNameLink) |
| 198 | +} else { |
| 199 | +linkerNameErr = os.Symlink(filepath.Base(dstPath), linkerNameLink) |
| 200 | +} |
| 201 | +} |
| 202 | + |
| 203 | +err = errors.Join(sonameErr, linkerNameErr) |
| 204 | +if err != nil { |
| 205 | +return fmt.Errorf("patchpkg: create symlinks for shared library: %w", err) |
| 206 | +} |
| 207 | +return nil |
| 208 | +} |
| 209 | + |
| 210 | +func (lib SharedLibrary) LogValue() slog.Value { |
| 211 | +return slog.GroupValue( |
| 212 | +slog.String("lib.path", lib.Name()), |
| 213 | +slog.String("lib.linkername", lib.LinkerName), |
| 214 | +slog.String("lib.soname", lib.Soname), |
| 215 | +slog.String("lib.realname", lib.RealName), |
| 216 | +) |
| 217 | +} |
0 commit comments