How do they differ?
I read that SUBALIGN() somehow forces a certain alignment. Are there other differences?
When should I use ALIGN() and when should I use SUBALIGN()?
SUBALIGN is
specifically for adjusting the alignment of the input sections within an output section.
To illustrate:
$ cat one.c
char a_one __attribute__((section(".mysection"))) = 0;
char b_one __attribute__((section(".mysection"))) = 0;
$ cat two.c
char a_two __attribute__((section(".mysection"))) = 0;
char b_two __attribute__((section(".mysection"))) = 0;
$ gcc -c one.c two.c
Case 1
$ cat foo_1.lds
SECTIONS
{
. = 0x10004;
.mysection ALIGN(8) : {
*(.mysection)
}
}
$ ld -T foo_1.lds one.o two.o -o foo1.out
$ readelf -s foo1.out
Symbol table '.symtab' contains 9 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000010008 0 SECTION LOCAL DEFAULT 1
2: 0000000000000000 0 SECTION LOCAL DEFAULT 2
3: 0000000000000000 0 FILE LOCAL DEFAULT ABS one.c
4: 0000000000000000 0 FILE LOCAL DEFAULT ABS two.c
5: 000000000001000b 1 OBJECT GLOBAL DEFAULT 1 b_two
6: 0000000000010008 1 OBJECT GLOBAL DEFAULT 1 a_one
7: 0000000000010009 1 OBJECT GLOBAL DEFAULT 1 b_one
8: 000000000001000a 1 OBJECT GLOBAL DEFAULT 1 a_two
$ readelf -t foo1.out | grep -A3 mysection
[ 1] .mysection
PROGBITS PROGBITS 0000000000010008 0000000000010008 0
0000000000000004 0000000000000000 0 1
[0000000000000003]: WRITE, ALLOC
Here, ALIGN(8) aligns .mysection to the next 8-byte boundary, 0x10008,
after 0x10004.
The char symbol a_one, coming from input section one.o(.mysection), is at the start of .mysection
followed at the next byte by b_two, also coming from input section one.o(.mysection). At the next byte,
is a_two, from input section two.o(.mysection), then b_two, also from two.o(.mysection). All 4
objects from all input sections *(.mysection) are just placed end to end from the start of output section .mysection.
Case 2
$ cat foo_2.lds
SECTIONS
{
. = 0x10004;
.mysection ALIGN(8) : SUBALIGN(16) {
*(.mysection)
}
}
$ ld -T foo_2.lds one.o two.o -o foo2.out
$ readelf -s foo2.out
Symbol table '.symtab' contains 9 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000010008 0 SECTION LOCAL DEFAULT 1
2: 0000000000000000 0 SECTION LOCAL DEFAULT 2
3: 0000000000000000 0 FILE LOCAL DEFAULT ABS one.c
4: 0000000000000000 0 FILE LOCAL DEFAULT ABS two.c
5: 0000000000010021 1 OBJECT GLOBAL DEFAULT 1 b_two
6: 0000000000010010 1 OBJECT GLOBAL DEFAULT 1 a_one
7: 0000000000010011 1 OBJECT GLOBAL DEFAULT 1 b_one
8: 0000000000010020 1 OBJECT GLOBAL DEFAULT 1 a_two
$ readelf -t foo2.out | grep -A3 mysection
[ 1] .mysection
PROGBITS PROGBITS 0000000000010008 0000000000010008 0
000000000000001a 0000000000000000 0 16
[0000000000000003]: WRITE, ALLOC
This time, the 8-byte aligned address of .mysection is unchanged. But the
effect of SUBALIGN(16) is that symbol a_one, coming from input
section one.o(.mysection) is placed at the next 16-byte
boundary, 0x10010, after the start of .mysection, and symbol b_one, coming from the
same input section is at the next byte. But symbol a_two, coming from input section
two.o(.mysection) is at the next 16-byte boundary, 0x10020; and b_two, coming
also from two.o(.mysection), is 1 byte after that.
Related
I have an SPI bus between a MAX V device and an AM335x processor.
The MAX V device has an SPI setup to repeatedly send a STD_LOGIC_VECTOR defined as "x0100".
This seems to work fine. The output on a scope is repeatedly the same value.
In Linux, I seem to get either shifted data, or some random data. Using spi-tools from here https://github.com/cpb-/spi-tools
When these tools are used, I get the following:
# spi-config -d /dev/spidev0.0 -m 1 -s 10000000
# spi-pipe -d /dev/spidev0.0 -b 2 -n 1 < /dev/urandom | hexdump
0000000 0202
0000002
# spi-pipe -d /dev/spidev0.0 -b 2 -n 1 < /dev/urandom | hexdump
0000000 0a0a
0000002
# spi-pipe -d /dev/spidev0.0 -b 2 -n 1 < /dev/urandom | hexdump
0000000 2a2a
0000002
# spi-pipe -d /dev/spidev0.0 -b 2 -n 1 < /dev/urandom | hexdump
0000000 aaaa
0000002
# spi-pipe -d /dev/spidev0.0 -b 2 -n 1 < /dev/urandom | hexdump
0000000 aaaa
0000002
You can see how the device is configured there. On the scope, the MISO pin is clearly outputting "00000010 00000000" for every 16 clock cycles on SCLK. What is happening here? How can I repeatedly get the correct value from the device?
For clarity, here is the relevant parts of the device tree and the kernel configuration.
Kernel
CONFIG_SPI=y
CONFIG_SPI_MASTER=y
CONFIG_SPI_GPIO=y
CONFIG_SPI_BITBANG=y
CONFIG_SPI_OMAP24XX=y
CONFIG_SPI_TI_QSPI=y
CONFIG_SPI_SPIDEV=y
CONFIG_REGMAP_SPI=y
CONFIG_MTD_SPI_NOR=y
CONFIG_SPI_CADENCE_QUADSPI=y
Device Tree
&spi1 {
/* spi1 bus is connected to the CPLD only on CS0 */
status = "okay";
pinctrl-names = "default";
pinctrl-0 = <&spi1_pins>;
ti,pindir-d0-out-d1-in;
cpld_spidev: cpld_spidev#0 {
status = "okay";
compatible = "linux,spidev";
spi-max-frequency = <1000000>;
reg = <0>;
};
};
Also here is a screengrab of the waveforms produced.
Really the end goal is an app to report the version stated as the STD_LOGIC_VECTOR, on the MAX V device. So 0100 is intended to be version 1.00.
Use the uboot_overlay in /boot/uEnv.txt called BB-SPIDEV0-00A0.dtbo.
If you need any more info, please ask. Oh! And there is a fellow, Dr. Molloy, that had produced a book a while back.
chp08/spi/ is the location of the file you will need to test the SPI Device.
The command is simply spidev_test
In Jonesforth, a dictionary entry is laid out as follows:
<--- DICTIONARY ENTRY (HEADER) ----------------------->
+------------------------+--------+---------- - - - - +----------- - - - -
| LINK POINTER | LENGTH/| NAME | DEFINITION
| | FLAGS | |
+--- (4 bytes) ----------+- byte -+- n bytes - - - - +----------- - - - -
We can take a peek at one of these entries using GDB. (See this question for details on using GDB with Jonesforth.)
Let's display the first 16 bytes of the dictionary entry for SWAP as characters:
>>> x/16cb &name_SWAP
0x105cc: -68 '\274' 5 '\005' 1 '\001' 0 '\000' 4 '\004' 83 'S' 87 'W' 65 'A'
0x105d4: 80 'P' 0 '\000' 0 '\000' 0 '\000' 43 '+' 0 '\000' 1 '\001' 0 '\000'
You can kind of see what's going on here.
The first four bytes are the pointer to the previous word in the dictionary:
-68 '\274' 5 '\005' 1 '\001' 0 '\000'
Then comes the length of the name:
4 '\004'
Then we see the characters of the word name, "SWAP":
83 'S' 87 'W' 65 'A' 80 'P'
And finally some padding to align on a 32-bit boundary:
0 '\000' 0 '\000' 0 '\000'
It would be nice if there was a way to format the word entry in a nicer manner.
If we do the following:
>>> x/1xw &name_SWAP
0x105cc: 0x000105bc
we note that name_SWAP is at 0x105cc.
Let's use GDB's printf to display the word entry:
>>> printf "link: %#010x name length: %i name: %s\n", *(0x105cc), (char)*(0x105cc+4), (0x105cc+5)
link: 0x000105bc name length: 4 name: SWAP
OK, that's not bad! We see the link, the name length, and name, all nicely displayed and labeled.
The downside here is that I have to use the explicit address in the call to printf:
printf "link: %#010x name length: %i name: %s\n", *(0x105cc), (char)*(0x105cc+4), (0x105cc+5)
Ideally, I'd just be able to say something like:
show_forth_word name_SWAP
and it'd display the above.
What's the best way to go about this? Is this doable with a GDB user-defined command? Or is it something more appropriate for the GDB Python interface?
My question is, what's the best way to go about this?
It depends on whether GDB knows about the type of name_SWAP. If it does, the Python pretty-printer is the way to go.
If it doesn't, something as simple as user-defined command is likely easier. Assuming 32-bit mode:
define print_key
set var $v = (char*)$arg0
printf "link: %#010x name length: %i name: %s\n", *((char**)$v), *($v+4), ($v+5)
end
I just used nm , the good old Unix command.
I have a log file with lots of unnecessary information. The only important part of that file is a table which describes some statistics. My goal is to have a script which will accept a column name as argument and return the sum of all the elements in the specified column.
Example log file:
.........
Skipped....
........
WARNING: [AA[409]: Some bad thing happened.
--- TOOL_A: READING COMPLETED. CPU TIME = 0 REAL TIME = 2
--------------------------------------------------------------------------------
----- TOOL_A statistics -----
--------------------------------------------------------------------------------
NAME Attr1 Attr2 Attr3 Attr4 Attr5
--------------------------------------------------------------------------------
AAA 885 0 0 0 0
AAAA2 1 0 2 0 0
AAAA4 0 0 2 0 0
AAAA8 0 0 2 0 0
AAAA16 0 0 2 0 0
AAAA1 0 0 2 0 0
AAAA8 0 0 23 0 0
AAAAAAA4 0 0 18 0 0
AAAA2 0 0 14 0 0
AAAAAA2 0 0 21 0 0
AAAAA4 0 0 23 0 0
AAAAA1 0 0 47 0 0
AAAAAA1 2 0 26 0
NOTE: Some notes
......
Skipped ......
The expected usage script.sh Attr1
Expected output:
888
I've tried to find something with sed/awk but failed to figure out a solution.
tldr;
$ cat myscript.sh
#!/bin/sh
logfile=${1}
attribute=${2}
field=$(grep -o "NAME.\+${attribute}" ${logfile} | wc -w)
sed -nre '/NAME/,/NOTE/{/NAME/d;/NOTE/d;s/\s+/\t/gp;}' ${logfile} | \
cut -f${field} | \
paste -sd+ | \
bc
$ ./myscript.sh mylog.log Attr3
182
Explanation:
assign command-line arguments ${1} and ${2} to the logfile and attribute variables, respectively.
with wc -w, count the quantity of words within the line that
contains both NAME and ${attribute} (the field index) and assign it to field
with sed
suppress automatic printing (-n) and enable extended regular expressions (-r)
find lines between the NAME and NOTE lines, inclusive
delete the lines that match NAME and NOTE
translate each contiguous run of whitespace to a single tab and print the result
cut using the field index
paste all numbers as an infix summation
evaluate the infix summation via bc
Quick and dirty (without any other spec)
awk -v CountCol=2 '/^[^[:blank:]]/ && NF == 6 { S += $( CountCol) } END{ print S + 0 }' YourFile
with column name
awk -v ColName='Attr1' '/^[[:blank:]]/ && NF == 6 { for(i=1;i<=NF;i++){if ( $i == ColName) CountCol = i } /^[^[:blank:]]/ && NF == 6 && CountCol{ S += $( CountCol) } END{ print S + 0 }' YourFile
you should add a header/trailer filter to avoid noisy line (a flag suit perfect for this) but lack of info about structure to set this flag, i use sthe simple field count (assuming text field have 0 as value so not changing the sum when taken in count)
$ awk -v col='Attr3' '/NAME/{for (i=1;i<=NF;i++) f[$i]=i} col in f{sum+=$(f[col]); if (!NF) {print sum+0; exit} }' file
182
I have 2 files of which I currently manipulate each one in awk:
======================= File 1: ===================
0x0002 RUNNING EXISTS foo 253 65535
0x0003 RUNNING EXISTS foo 252 5
0x0004 RUNNING EXISTS foo 251 3
I'm interested in the first field and the last 2.
Field 1: vdisk(in hex). Last two fields are the possible Cdisks for each vdisk. At least 1 must exist. the values are decimal.
If the number "65535" appears, it means that the 2nd cdisk is non-existent.
I use this awk to display a user friendly table:
awk 'BEGIN {print "vdisk cdisk Mr_cdisk"}
{
if ( $3 ~ /EXISTS|THIS_AGENT_ONLINE/ ) {
sub("65535", "N/A")
printf "%-11s %-6s %s\n",$1,$(NF-1),$(NF)
}
}' ${FILE}
Will produce this table:
vdisk cdisk Mr_cdisk
0x0002 253 N/A
0x0003 252 5
0x0004 1 3
======================= File 2: ===================
0x0000 Cmp cli Foo 0 SOME 0 0x0 0x0 0x0
0x0001 Cmp own Foo 1 NONE 0 0x0 0x0 0x0
0x0002 Cmp cli Foo 0 SOME 0 0x0 0x1 0x0
0x0003 Cmp own Foo 0 NONE 0 0x0 0x0 0x1
0x0004 Cmp cli Foo 0 SOME 0 0x0 0x0 0x0
0x0005 Cmp own Foo 1 NONE 0 0x1 0x0 0x0
I'm interested in the "Cmp own" lines, in which the first field is the Cdisk (in hex). The 5th field from the end (just before the SOME/NONE text), is the instance number. It's either 0 or 1.
I use this awk to display a user friendly table:
awk 'BEGIN {print "cdisk(hex) RACE_Instance"}
/Cmp own/ {
printf "%-11s %-10s\n",$1,$(NF-5)
}' ${FILE};
This will produce the following table:
cdisk(hex) Instance
0x0001 1
0x0003 0
0x0005 1
++++++++++++++++++++++++++++++++++++++
What would I like to display a merged table. Preferably, directly from the original files.
It should spread the first data into 2 lines (if there's more than 1 cdisk). This will be the base for the merge. Then print the Instance number, if exist per this cdisk.
vdisk(hex) cdisk(hex) Instance
0x0002 0x00fd N/A
0x0003 0x00fc N/A
0x0003 0x0005 1
0x0004 0x0001 0
0x0004 0x0003 1
I would definitely prefer a solution with awk. :)
Thanks!
EDIT: added some more info and correction to one data table.
EDIT2: Simplified input
I couldn't figure out what the mapping is from your 2 input files to your output but this should point you in the right direction:
$ cat tst.awk
NR==FNR {
v2c[$1] = sprintf("0x%04x",$5)
v2m[$1] = ( $6==65535 ? "N/A" : sprintf("0x%04x",$6) )
next
}
$1 in v2c {
print $1, v2c[$1], $5
print $1, v2m[$1], $5
}
$
$ awk -f tst.awk file1 file2
0x0002 0x00fd 0
0x0002 N/A 0
0x0003 0x00fc 0
0x0003 0x0005 0
0x0004 0x00fb 0
0x0004 0x0003 0
I need a regex to match a string that:
has only digits 0-9 and spaces
all digits must be same
should have at-least 2 digits
should start and end with digits
Matches:
11
11111
1 1 1 1 1
1 1
11 1 1 1 1 1
1 1
1 1 1
No matches:
1 has only one digit
11111 has space at the end
11111 has space at beginning
12 digits are different
11: has other character
I know regex for each of my requirement. That way I'll use 4 regex
tests. Can we do it in one regex?
Yes it can be done in one regex:
^(\d)(?:\1| )*\1$
Rubular link
Explanation:
^ - Start anchor
( - Start parenthesis for capturing
\d - A digit
) - End parenthesis for capturing
(?: - Start parenthesis for grouping only
\1 - Back reference referring to the digit capture before
| - Or
- A literal space
) - End grouping parenthesis
* - zero or more of previous match
\1 - The digit captured before
$ - End anchor
Consider this program:
#!/usr/bin/perl -l
$_ = "3 33 3 3";
print /^(\d)[\1 ]*\1$/ ? 1 : 0;
print /^(\d)(?:\1| )*\1$/ ? 1 : 0;
It produces the output
0
1
The answer is obvious when you look at the compiled regexes:
perl -c -Mre=debug /tmp/a
Compiling REx "^(\d)[\1 ]*\1$"
synthetic stclass "ANYOF[0-9][{unicode_all}]".
Final program:
1: BOL (2)
2: OPEN1 (4)
4: DIGIT (5)
5: CLOSE1 (7)
7: STAR (19)
8: ANYOF[\1 ][] (0)
19: REF1 (21)
21: EOL (22)
22: END (0)
floating ""$ at 1..2147483647 (checking floating) stclass ANYOF[0-9][{unicode_all}] anchored(BOL) minlen 1
Compiling REx "^(\d)(?:\1| )*\1$"
synthetic stclass "ANYOF[0-9][{unicode_all}]".
Final program:
1: BOL (2)
2: OPEN1 (4)
4: DIGIT (5)
5: CLOSE1 (7)
7: CURLYX[1] {0,32767} (17)
9: BRANCH (12)
10: REF1 (16)
12: BRANCH (FAIL)
13: EXACT < > (16)
15: TAIL (16)
16: WHILEM[1/1] (0)
17: NOTHING (18)
18: REF1 (20)
20: EOL (21)
21: END (0)
floating ""$ at 1..2147483647 (checking floating) stclass ANYOF[0-9][{unicode_all}] anchored(BOL) minlen 1
/tmp/a syntax OK
Freeing REx: "^(\d)[\1 ]*\1$"
Freeing REx: "^(\d)(?:\1| )*\1$"
Backrefs are just regular octal characters inside character classes!!
^(\d)( *\1)+$
/^(\d)(\1| )*\1$/