How do I get the real position of a section in an ELF archive file? - elf

I want to extract a binary section fron a .a ELF archive file in Linux.
When I run objdump -h on an archive file, it lists the object files it contains, and the section headers for each section. However, the File Offset column appears to be relative to the object file position in the archive, as otherwise they would all the sections would be overlapping.
I expected I could use dd to extract binary information from the archive file. (see How do you extract only the contents of an ELF section). How do I do this with an archive?
I should also mention the section I'm extracting is added with this command :
echo "hi" > commentFile
objcopy libmylib.a --add-section .mysection=commentFile libmylib.a

the File Offset column appears to be relative to the object file position in the archive
The file offset you get from objdump is relative to the beginning of the individual object file. You can think of an archive library as a bookshelf, and the ouput of objdump -h as the index within each individual book. You wouldn't expected the index to change depending on which other books are on the shelf, or when you take the book from the shelf. Similarly, the object file itself (and the output of objdump -h) does not change when you put into the library, or extract it out again (you get bit-identical copy).
I expected I could use dd to extract binary information from the archive file
You could use dd, but you'd have to first find the position of each individual object file in the archive. That's not too difficult: the format of UNIX archive files is documented. But the format can change depending on which UNIX variant you use, and it's not really necessary for the task you want to perform.
How do I do this with an archive?
If you know that .mysection has identical contents in all object files in libmylib.a (as would be the case for the objcopy --add-section command you gave), then extract one object, from the archive, then extract the section:
firstobj=$(ar t libmylib.a | grep '\.o$' | head -1)
ar x libmylib.a $firstobj
# use objdump -h and dd to extract section context.
# or use "readelf -p .mysection $firstobj
rm -f $firstobj
If the contents of .mysection may be different in different object files, extract them to a temporary directory:
mkdir tmp.$$ && cd tmp.$$ && ar x ../libmylib.a
for obj in $(find . -type f); do
# extract .mysection from $obj
done
cd .. && rm -rf tmp.$$

Related

add_custom_target multiple dependencies provided by a list

set(OUTPUT_PATH "some_path/some_path2/")
set(NAME_XML "external/some_folder/somexml.xml")
set(OUTPUT_DIRECTORY "header1.h" "header2.h" "header3.h")
add_custom_target(
some_target ALL
DEPENDS ${OUTPUT_PATH}header1.h
DEPENDS ${OUTPUT_PATH}header2.h
......
)
foreach(item ${OUTPUT_DIRECTORY})
message(STATUS "testing..." ${item})
add_custom_command(
COMMAND python3 ${OUTPUT_PATH}/main.py -n "1" -p "${OUTPUT_PATH}" -f "${NAME_XML}" -o "${item}"
DEPENDS ${NAME_XML}
OUTPUT ${OUTPUT_PATH}${item}
COMMENT "some comment: ${item}"
)
endforeach(item)
The goal of this work is for python script to be called if the header file is not found (for each case) or has been modified. Similarly, if the XML file has been modified, I want to regenerate all header files by calling the python script.
The python script allows us to pass individual header files to be generated which is why I have this "foreach". As a result, I want to call it only per the requirements in the above paragraph.
How I can modify the code to achieve that and how I can include the OUTPUT_DIRECTORY as a list in the add_custom_target rather than adding DEPENDS in each line individually as per my code example?

Rename ttf/woff/woff2 file to PostScript Font Name with Script

I am a typographer working with many fonts that have incorrect or incomplete filenames. I am on a Mac and have been using Hazel, AppleScript, and Automator workflows, attempting to automate renaming these files*. I require a script to replace the existing filename of ttf, woff, or woff2 files in Finder with the font's postscriptName. I know of tools (fc-scan/fontconfig, TTX, etc) which can retrieve the PostScript name-values I require, but lack the programming knowhow to code a script for my purposes. I've only managed to setup a watched directory that can run a script when any files matching certain parameters are added.
*To clarify, I am talking about changing the filename only, not the actual names stored within the font. Also I am open to a script of any compatible language or workflow of scripts if possible, e.g. this post references embedding AppleScript within Shell scripts via osascript.
StackExchange Posts I've Consulted:
How to get Fontname from OTF or TTF File?
How to get PostScript name of TTF font in OS X?
How to Change Name of Font?
Automate Renaming Files in macOS
Others:
https://github.com/dtinth/JXA-Cookbook/wiki/Using-JavaScript-for-Automation
https://github.com/fonttools/fonttools
https://github.com/devongovett/fontkit
https://www.npmjs.com/package/rename-js
https://opentype.js.org/font-inspector.html
http://www.fontgeek.net/blog/?p=343
https://www.lantean.co/osx-renaming-fonts-for-free
Edit: Added the following by request.
1) Screenshot of a somewhat typical webfont, illustrating how the form fields for font family and style names are often incomplete, blank, or contain illegal characters.
2) The woff file depicted (also, as base64).
Thank you all in advance!
Since you mentioned Automator in your question, I thought I'd try and solve this while using that to rename the file, along with standard Mac bash to get the font name. Hopefully, it beats learning a whole programming language.
I don't know what your workflow is so I'll leave any deviations to you but here is a method to select a font file and from Services, rename the file to the font's postscript name… based on Apple's metadata, specifically "com_apple_ats_name_postscript". This is one of the pieces of data retrieved using 'mdls' from the Terminal on the font file. To focus on the postscript name, grep the output for name_postscript. For simplicity here, I'll exclude the path to the selected file.
Font Name Aquisition
So… running this command…
mdls GenBkBasBI.ttf | grep -A1 name_postscript
… generates this output, which contains FontBook's Postscript name. The 'A1' in grep returns the found line and the first line after, which is the one containing the actual font name.
com_apple_ats_name_postscript = (
"GentiumBookBasic-BoldItalic"
Clean this up with some more bash (tr, tail)…
tr -d \ | tail -n 1 | tr -d \"
In order, these strip spaces, all lines excepting the last, and quotation marks. So for the first 'tr' instance, there is an extra space after the backslash.
In a single line, it looks like this…
mdls GenBkBasBI.ttf | grep -A1 name_postscript | tr -d \ | tail -n 1 | tr -d \"
…and produces this…
GentiumBookBasic-BoldItalic
Now, here is the workflow that includes the above bash command. I got the idea for variable usage from the answer to this question…
Apple Automator “New PDF from Images” maintaining same filename
Automator Workflow
Automator Workflow screenshot
At the top; Service receives selected 'files or folders' in 'Finder'.
Get Selected Finder Items
This (or Get Specified…) is there to allow testing. It is obviated by using this as a Service.
Set Value of Variable (File)
This is to remember which file you want to rename
Run Shell Script
This is where we use the bash stuff. The $f is the selected/specified file. I'm running 'zsh' for whatever reason. You can set it to whatever you're running, presumably 'bash'.
Set Value of Variable (Text)
Assign the bash output to a variable. This will be used by the last action for the new filename.
Get Value of Variable (File)
Recall the specified/selected file to rename.
Rename Finder Items: Name Single Item
I have it set to 'Basename only' so it will leave the extension alone. Enter the 'Text' variable from action 4 in here.

How to get a list of files modified since date/revision in Accurev

I have created a workspace backed by some collaboration stream. The stream is updated regularly by team members. My goal is to take modified files in a given path and put them to another repository (do it regularly).
The question is how to create a list of files which were modified since a revision or date or ..? (I don't know which approach is the best.) The command line is preferable.
Once I get the file list I create an automating script to take the files from one place and put them to another.
accurev hist -s Your_Stream -t "2013/05/16 01:00:00"-now -a -fl
You can run accurev stat -m -fx and then parse resulting XML. element elements will have modTime attribute, which is the UNIX timestamp when the file was modified.

Trying to cat a header into source files but a Unicode BOM is getting in the way

Following the instructions at Add header (copyright) information to existing source files, I need to add copyright headers to a bunch of source files we're sending out of the building. (I know, I hate copyright headers too, but it's policy for when we release proprietary source files. Please consider "persuade someone to waive the policy" as unhelpful and as not answering the question.)
I have two copies of all the files (in dir and dir.orig) and, from within dir.orig, I'm using
find . -name \*.cs -exec sh -c "mv '{}' tmp && cp ../header.txt '../dir/{}'
&& cat tmp >> '../dir/{}' && rm tmp" \;
This is working, but it's ending up with the header, then the BOM from the original source file, whereas I'd prefer either the BOM to move to the start or be removed.
(Looking at this, I realise that moving the file to tmp is unnecessary, given I'm not overwriting the original, but I didn't bother removing that from the example from the other SO question.)
How can I remove (or move) the BOM so that I end up without it appearing immediately after the newly-added header?
I think I may have found my solution, thanks to being pointed to uconv from this answer from Steven R. Loomis on a related question.
If I use
find . -name *.cs -exec sh -c "cp ../header.txt '../dir/{}'
&& uconv --remove-signature -f UTF-8 -t UTF-8 '{}' >> '../dir/{}'" \;
, then uconv assumes both input (-f) and output (-t) encodings should be UTF-8, but --remove-signature causes it to remove any BOM it finds.

cmake move directory

I was wondering if there was a way (such as a commad) to move a directory filled with, say, image files, to the build directory using cmake 2.8.
Thanks in advance!
The file() command can do what you want.
From the cmake manual:
The file() command also provides COPY and INSTALL signatures:
file(<COPY|INSTALL> files... DESTINATION <dir>
[FILE_PERMISSIONS permissions...]
[DIRECTORY_PERMISSIONS permissions...]
[NO_SOURCE_PERMISSIONS] [USE_SOURCE_PERMISSIONS]
[FILES_MATCHING]
[[PATTERN <pattern> | REGEX <regex>]
[EXCLUDE] [PERMISSIONS permissions...]] [...])
The COPY signature copies files, directories, and symlinks to a destination fold Relative input paths are evaluated with respect to the current source directory, and a relative destination is evaluated with respect to the current build directory. Copying preserves input file timestamps, and optimizes out a file if it exists at the destination with the same timestamp. Copying preserves input permissions unless explicit permissions or NO_SOURCE_PERMISSIONS are given (default is USE_SOURCE_PERMISSIONS). See the install(DIRECTORY) command for documentation of permissions, PATTERN, REGEX, and EXCLUDE options.
So you would have something like (tested):
file(COPY ${YOUR_SRC_IMAGE_DIR} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/YourPreferedDestination)
To move, you can use the RENAME form:
file(RENAME ${YOUR_SRC_IMAGE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/PreferedDestination)
But I am not sure that you would want that, because the source will not be available anymore to reproduce the build sequence, hence my attempt to answer with the copy command above.