#!/opt/Lua51/bin/lua -- Created by jiaqiang.ye@lnxall.com -- Strip ELF binary files -- 2025/08/13 package.cpath = "/opt/Lua51/lib/lua/?.so;/opt/Lua51/lib/lua/loadall.so" package.path = "/opt/Lua51/share/lua/?.lua;/opt/Lua51/share/lua/?/init.lua;/opt/Lua51/lib/lua/?.lua;/opt/Lua51/lib/lua/?/init.lua" local invoker = require 'invoker' local gfmt = string.format local g_STRIP = nil local g_ELF4B = string.char(0x7f) .. "ELF" local g_SKIPLIST = { ['proto_forward'] = 1, -- new_ems ['alarm'] = 1, ['config'] = 1, ['device_manager'] = 1, ['emsd'] = 1, ['northd'] = 1, ['procdata'] = 1, ['protod'] = 1, ['libloadcfg.so'] = 1, ['libnngmsg.so'] = 1, } local function strip_file(filp) if invoker.readfile(filp, nil, 0x4) ~= g_ELF4B then return false end local bname = string.match(filp, "/([^/]+)$") if type(bname) ~= "string" or #bname == 0 then bname = filp end if g_SKIPLIST[bname] then io.stdout:write(gfmt("INFO: stripping ELF file: %s => skipped.\n", filp)) io.stdout:flush() return true end io.stdout:write(gfmt("INFO: stripping ELF file: %s ...\n", filp)) io.stdout:flush() return invoker.invoke(0, g_STRIP, "-g", filp) == 0 end local function glob_files(dirp) local fcmd = gfmt("exec find '%s' -type f -print", dirp) local fout = io.popen(fcmd, "r") if not fout then io.stderr:write(gfmt("Error, failed to execute command: %s\n", fcmd)) io.stderr:flush() return false end while true do local nl = fout:read("*l") if nl == nil then break end local typf = invoker.statfile(nl, true) if typf == "regular" then strip_file(nl) elseif typf == "directory" then io.stderr:write(gfmt("Error, file path is a directory: %s\n", nl)) io.stderr:flush() elseif typf == nil then io.stderr:write(gfmt("Error, file path not found: %s\n", nl)) io.stderr:flush() end end fout:read("*a") fout:close() return true end local function mainfunc() local strip = os.getenv('LNX_TCSTRIP') if type(strip) ~= "string" or #strip == 0 then io.stderr:write("ERROR: \`LNX_TCSTRIP not defined; cannot strip ELF binaries.\n") io.stderr:flush() os.exit(1) end g_STRIP = strip local idx = 0 while true do idx = idx + 1 local argn = arg[idx] if type(argn) ~= "string" then break end local typf = invoker.statfile(argn, true) if typf == "regular" then strip_file(argn) elseif typf == "directory" then glob_files(argn) elseif typf == nil then io.stderr:write(gfmt("Error, file path not found: %s\n", argn)) io.stderr:flush() end end end mainfunc() os.exit(0)