Skip to contentSkip to conversation

fs/squashfs: use do_div function for math

PATCH Superseded Sean Nyekjaer 5 participants 11 messages May 5, 2022 – May 12, 2022 SquashFS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/squashfs: use do_div function for math

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to __udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to __udivmoddi4'

---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +3 -2
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 	}
 
 	for (j = 0; j < datablk_count; j++) {
-		start = data_offset / ctxt.cur_dev->blksz;
+		start = do_div(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 		goto out;
 	}
 
-	start = frag_entry.start / ctxt.cur_dev->blksz;
+	start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
Signature · 1 line
2.35.1
#1 lore raw
Quoted 34 lines · Sean Nyekjaer

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to __udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to __udivmoddi4'

---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +2 -1
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 	}
 
 	for (j = 0; j < datablk_count; j++) {
-		start = data_offset / ctxt.cur_dev->blksz;
+		start = do_div(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, · line 1523

Quoted 8 lines
fs/squashfs/sqfs.c +1 -1
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 		goto out;
 	}
 
-	start = frag_entry.start / ctxt.cur_dev->blksz;
+	start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);

Adding maintainers...

Signature · 1 line
Tom
Reviewed-by
#2 lore raw

Hello,

[email protected] wrote on Thu, 5 May 2022 09:15:00 -0400:

Quoted 39 lines · Sean Nyekjaer
Quoted 38 lines

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to __udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to __udivmoddi4'

---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +2 -1
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 	}
 
 	for (j = 0; j < datablk_count; j++) {
-		start = data_offset / ctxt.cur_dev->blksz;
+		start = do_div(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 		goto out;
 	}
 
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, · line 1523

Quoted 5 lines
fs/squashfs/sqfs.c +1 -1
-	start = frag_entry.start / ctxt.cur_dev->blksz;
+	start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);  

Adding maintainers...

I guess that's a correct fix, so

Thanks,
Miquèl

#3 lore raw
Quoted 51 lines · On Monday 09 May 2022 15:08:15 Miquel Raynal wrote:

Hello,

[email protected] wrote on Thu, 5 May 2022 09:15:00 -0400:

Quoted 39 lines · Sean Nyekjaer
Quoted 38 lines

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to __udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to __udivmoddi4'

---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +2 -1
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 	}
 
 	for (j = 0; j < datablk_count; j++) {
-		start = data_offset / ctxt.cur_dev->blksz;
+		start = do_div(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 		goto out;
 	}
 
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, · line 1523

Quoted 5 lines
fs/squashfs/sqfs.c +1 -1
-	start = frag_entry.start / ctxt.cur_dev->blksz;
+	start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);  

Adding maintainers...

Quoted 6 lines

I guess that's a correct fix, so

Thanks,
Miquèl

Hello! do_div() macro modifies its first argument. It is correct? Because previous code did not modify anything.

#4 lore raw
Quoted 54 lines · On Thursday 12 May 2022 12:19:01 Pali Rohár wrote:
Quoted 53 lines · On Monday 09 May 2022 15:08:15 Miquel Raynal wrote:

Hello,

[email protected] wrote on Thu, 5 May 2022 09:15:00 -0400:

Quoted 39 lines · Sean Nyekjaer
Quoted 38 lines

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to __udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to __udivmoddi4'

---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +2 -1
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 	}
 
 	for (j = 0; j < datablk_count; j++) {
-		start = data_offset / ctxt.cur_dev->blksz;
+		start = do_div(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 		goto out;
 	}
 
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, · line 1523

Quoted 5 lines
fs/squashfs/sqfs.c +1 -1
-	start = frag_entry.start / ctxt.cur_dev->blksz;
+	start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);  

Adding maintainers...

I guess that's a correct fix, so

Quoted 7 lines
Quoted 4 lines

Thanks,
Miquèl

Hello! do_div() macro modifies its first argument. It is correct? Because previous code did not modify anything.

* The semantics of do_div() are:
*
* u32 do_div(u64 *n, u32 base)
* {
* u32 remainder = *n % base;
* *n = *n / base;
* return remainder;
* }

And this patch replaced:

  start = data_offset / ctxt.cur_dev->blksz;

by:

  start = do_div(data_offset, ctxt.cur_dev->blksz);

which expands to:

  start = data_offset % ctxt.cur_dev->blksz;
  data_offset = data_offset / ctxt.cur_dev->blksz;

Which is wrong as it calculates something totally different.

So this patch is incorrect.

#5 lore raw
Quoted 80 lines · Pali Rohár
Quoted 54 lines · On Thursday 12 May 2022 12:19:01 Pali Rohár wrote:
Quoted 53 lines · On Monday 09 May 2022 15:08:15 Miquel Raynal wrote:

Hello,

[email protected] wrote on Thu, 5 May 2022 09:15:00 -0400:

Quoted 39 lines · Sean Nyekjaer
Quoted 38 lines

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to __udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to __udivmoddi4'

---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +2 -1
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 	}
 
 	for (j = 0; j < datablk_count; j++) {
-		start = data_offset / ctxt.cur_dev->blksz;
+		start = do_div(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 		goto out;
 	}
 
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, · line 1523

Quoted 5 lines
fs/squashfs/sqfs.c +1 -1
-	start = frag_entry.start / ctxt.cur_dev->blksz;
+	start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);  

Adding maintainers...

I guess that's a correct fix, so

Quoted 7 lines
Quoted 4 lines

Thanks,
Miquèl

Hello! do_div() macro modifies its first argument. It is correct? Because previous code did not modify anything.

* The semantics of do_div() are:
*
* u32 do_div(u64 *n, u32 base)
* {
* u32 remainder = *n % base;
* *n = *n / base;
* return remainder;
* }

And this patch replaced:

  start = data_offset / ctxt.cur_dev->blksz;

by:

  start = do_div(data_offset, ctxt.cur_dev->blksz);
Quoted 8 lines

which expands to:

  start = data_offset % ctxt.cur_dev->blksz;
  data_offset = data_offset / ctxt.cur_dev->blksz;

Which is wrong as it calculates something totally different.

So this patch is incorrect.

Hi Pali,

Yes I agree, I got tricked by some wrong cache from Yocto :/ I have tried lldiv(), and that works on our x86 target.

Do you have an opinion on that?
Or do you have another idea to avoid the
"undefined reference to `__udivmoddi4'" error?

/Sean

#6 lore raw
Quoted 90 lines · Sean Nyekjaer
Quoted 80 lines · Pali Rohár
Quoted 54 lines · On Thursday 12 May 2022 12:19:01 Pali Rohár wrote:
Quoted 53 lines · On Monday 09 May 2022 15:08:15 Miquel Raynal wrote:

Hello,

[email protected] wrote on Thu, 5 May 2022 09:15:00 -0400:

Quoted 39 lines · Sean Nyekjaer
Quoted 38 lines

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to __udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to __udivmoddi4'

---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +2 -1
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 	}
 
 	for (j = 0; j < datablk_count; j++) {
-		start = data_offset / ctxt.cur_dev->blksz;
+		start = do_div(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 		goto out;
 	}
 
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, · line 1523

Quoted 5 lines
fs/squashfs/sqfs.c +1 -1
-	start = frag_entry.start / ctxt.cur_dev->blksz;
+	start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);  

Adding maintainers...

I guess that's a correct fix, so

Quoted 7 lines
Quoted 4 lines

Thanks,
Miquèl

Hello! do_div() macro modifies its first argument. It is correct? Because previous code did not modify anything.

* The semantics of do_div() are:
*
* u32 do_div(u64 *n, u32 base)
* {
* u32 remainder = *n % base;
* *n = *n / base;
* return remainder;
* }

And this patch replaced:

  start = data_offset / ctxt.cur_dev->blksz;

by:

  start = do_div(data_offset, ctxt.cur_dev->blksz);
Quoted 8 lines

which expands to:

  start = data_offset % ctxt.cur_dev->blksz;
  data_offset = data_offset / ctxt.cur_dev->blksz;

Which is wrong as it calculates something totally different.

So this patch is incorrect.

Quoted 8 lines

Hi Pali,

Yes I agree, I got tricked by some wrong cache from Yocto :/ I have tried lldiv(), and that works on our x86 target.

Do you have an opinion on that?
Or do you have another idea to avoid the
"undefined reference to `__udivmoddi4'" error?

Please correct things to use lldiv(), thanks.

Signature · 1 line
Tom
#7 lore raw
In reply to Sean Nyekjaer
Quoted 92 lines · On Thursday 12 May 2022 20:02:33 Sean Nyekjaer wrote:
Quoted 80 lines · Pali Rohár
Quoted 54 lines · On Thursday 12 May 2022 12:19:01 Pali Rohár wrote:
Quoted 53 lines · On Monday 09 May 2022 15:08:15 Miquel Raynal wrote:

Hello,

[email protected] wrote on Thu, 5 May 2022 09:15:00 -0400:

Quoted 39 lines · Sean Nyekjaer
Quoted 38 lines

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to __udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to __udivmoddi4'

---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +2 -1
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 	}
 
 	for (j = 0; j < datablk_count; j++) {
-		start = data_offset / ctxt.cur_dev->blksz;
+		start = do_div(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 		goto out;
 	}
 
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, · line 1523

Quoted 5 lines
fs/squashfs/sqfs.c +1 -1
-	start = frag_entry.start / ctxt.cur_dev->blksz;
+	start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);  

Adding maintainers...

I guess that's a correct fix, so

Quoted 7 lines
Quoted 4 lines

Thanks,
Miquèl

Hello! do_div() macro modifies its first argument. It is correct? Because previous code did not modify anything.

* The semantics of do_div() are:
*
* u32 do_div(u64 *n, u32 base)
* {
* u32 remainder = *n % base;
* *n = *n / base;
* return remainder;
* }

And this patch replaced:

  start = data_offset / ctxt.cur_dev->blksz;

by:

  start = do_div(data_offset, ctxt.cur_dev->blksz);
Quoted 8 lines

which expands to:

  start = data_offset % ctxt.cur_dev->blksz;
  data_offset = data_offset / ctxt.cur_dev->blksz;

Which is wrong as it calculates something totally different.

So this patch is incorrect.

Hi Pali,

Quoted 8 lines

Yes I agree, I got tricked by some wrong cache from Yocto :/ I have tried lldiv(), and that works on our x86 target.

Do you have an opinion on that?
Or do you have another idea to avoid the
"undefined reference to `__udivmoddi4'" error?

/Sean

Hello! You really should not use C division operator for 64-bit integers as gcc still is not able to generate assembler code for such division on many 32-bit platforms.

You should use some function or macro from linux/math64.h or div64.h header file. For every combination of types, pairs and required operation there is specific function/macro (e.g. u64/u32, u64%u32, u64/u64, etc...).

I think that lldiv() is the correct call here. This function expects u64 as dividend, u32 as divisor and returns u64 result.

#8 lore raw
Quoted 106 lines · Pali Rohár
Quoted 92 lines · On Thursday 12 May 2022 20:02:33 Sean Nyekjaer wrote:
Quoted 80 lines · Pali Rohár
Quoted 54 lines · On Thursday 12 May 2022 12:19:01 Pali Rohár wrote:
Quoted 53 lines · On Monday 09 May 2022 15:08:15 Miquel Raynal wrote:

Hello,

[email protected] wrote on Thu, 5 May 2022 09:15:00 -0400:

Quoted 39 lines · Sean Nyekjaer
Quoted 38 lines

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to __udivmoddi4' ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to __udivmoddi4'

---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +2 -1
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 	}
 
 	for (j = 0; j < datablk_count; j++) {
-		start = data_offset / ctxt.cur_dev->blksz;
+		start = do_div(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 		goto out;
 	}
 
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, · line 1523

Quoted 5 lines
fs/squashfs/sqfs.c +1 -1
-	start = frag_entry.start / ctxt.cur_dev->blksz;
+	start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);  

Adding maintainers...

I guess that's a correct fix, so

Quoted 7 lines
Quoted 4 lines

Thanks,
Miquèl

Hello! do_div() macro modifies its first argument. It is correct? Because previous code did not modify anything.

* The semantics of do_div() are:
*
* u32 do_div(u64 *n, u32 base)
* {
* u32 remainder = *n % base;
* *n = *n / base;
* return remainder;
* }

And this patch replaced:

  start = data_offset / ctxt.cur_dev->blksz;

by:

  start = do_div(data_offset, ctxt.cur_dev->blksz);
Quoted 8 lines

which expands to:

  start = data_offset % ctxt.cur_dev->blksz;
  data_offset = data_offset / ctxt.cur_dev->blksz;

Which is wrong as it calculates something totally different.

So this patch is incorrect.

Hi Pali,

Quoted 8 lines

Yes I agree, I got tricked by some wrong cache from Yocto :/ I have tried lldiv(), and that works on our x86 target.

Do you have an opinion on that?
Or do you have another idea to avoid the
"undefined reference to `__udivmoddi4'" error?

/Sean

Hello! You really should not use C division operator for 64-bit integers as gcc still is not able to generate assembler code for such division on many 32-bit platforms.

Quoted 7 lines

You should use some function or macro from linux/math64.h or div64.h header file. For every combination of types, pairs and required operation there is specific function/macro (e.g. u64/u32, u64%u32, u64/u64, etc...).

I think that lldiv() is the correct call here. This function expects u64 as dividend, u32 as divisor and returns u64 result.

Thank you for the explanation :)
Updated patch should be on the list.

/Sean

#9 lore raw
In reply to Sean Nyekjaer

Hi

Il gio 12 mag 2022, 20:02 Sean Nyekjaer <[email protected]> ha scritto:

Quoted 94 lines · Pali Rohár
Quoted 6 lines · On Thursday 12 May 2022 12:19:01 Pali Rohár wrote:
Quoted 5 lines · On Monday 09 May 2022 15:08:15 Miquel Raynal wrote:
Quoted 4 lines

Hello,

[email protected] wrote on Thu, 5 May 2022 09:15:00 -0400:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Sean Nyekjaer
Quoted 6 lines

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to

`__udivmoddi4'

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to

`__udivmoddi4'

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Signed-off-by Sean Nyekjaer <

[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +0 -0
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void

Quoted 6 lines
fs/squashfs/sqfs.c +1 -0
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void

*buf, loff_t offset, loff_t len,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,

Quoted 7 lines
fs/squashfs/sqfs.c +1 -1
      }
 
      for (j = 0; j < datablk_count; j++) {
-             start = data_offset / ctxt.cur_dev->blksz;
+             start = do_div(data_offset, ctxt.cur_dev->blksz);
              table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
              table_offset = data_offset - (start *

ctxt.cur_dev->blksz);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void

Quoted 2 lines
fs/squashfs/sqfs.c +0 -0
              n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void

*buf, loff_t offset, loff_t len,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,

Quoted 7 lines
fs/squashfs/sqfs.c +1 -1
              goto out;
      }
 
-     start = frag_entry.start / ctxt.cur_dev->blksz;
+     start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
      table_size = SQFS_BLOCK_SIZE(frag_entry.size);
      table_offset = frag_entry.start - (start *

ctxt.cur_dev->blksz);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, · line 1447

Quoted 1 line
fs/squashfs/sqfs.c +0 -0
      n_blks = DIV_ROUND_UP(table_size + table_offset,

ctxt.cur_dev->blksz);

Quoted 35 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Adding maintainers...

I guess that's a correct fix, so

Quoted 7 lines
Quoted 4 lines

Thanks,
Miquèl

Hello! do_div() macro modifies its first argument. It is correct? Because previous code did not modify anything.

* The semantics of do_div() are:
*
* u32 do_div(u64 *n, u32 base)
* {

 *    u32 remainder = *n % base;
 *    *n = *n / base;
 *    return remainder;

* }

And this patch replaced:

  start = data_offset / ctxt.cur_dev->blksz;

by:

  start = do_div(data_offset, ctxt.cur_dev->blksz);

which expands to:

  start = data_offset % ctxt.cur_dev->blksz;
  data_offset = data_offset / ctxt.cur_dev->blksz;
Quoted 9 lines
Quoted 3 lines

Which is wrong as it calculates something totally different.

So this patch is incorrect.

Hi Pali,

Yes I agree, I got tricked by some wrong cache from Yocto :/ I have tried lldiv(), and that works on our x86 target.

start = data_offset;
do_div(start, ctxt.cur_dev->blksz);

Something like this?

Michael

Quoted 6 lines

Do you have an opinion on that?
Or do you have another idea to avoid the
"undefined reference to `__udivmoddi4'" error?

/Sean

#10 lore raw
Quoted 104 lines · On Thursday 12 May 2022 20:52:30 Michael Nazzareno Trimarchi wrote:

Hi

Il gio 12 mag 2022, 20:02 Sean Nyekjaer <[email protected]> ha scritto:

Quoted 100 lines · Pali Rohár
Quoted 6 lines · On Thursday 12 May 2022 12:19:01 Pali Rohár wrote:
Quoted 5 lines · On Monday 09 May 2022 15:08:15 Miquel Raynal wrote:
Quoted 4 lines

Hello,

[email protected] wrote on Thu, 5 May 2022 09:15:00 -0400:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Sean Nyekjaer
Quoted 6 lines

From: Sean Nyekjaer <[email protected]>

When compling for x86:
ld.bfd: fs/squashfs/sqfs.o: in function `sqfs_read':
u-boot/fs/squashfs/sqfs.c:1443: undefined reference to

`__udivmoddi4'

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

ld.bfd: u-boot/fs/squashfs/sqfs.c:1521: undefined reference to

`__udivmoddi4'

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Signed-off-by Sean Nyekjaer <

[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
---
 fs/squashfs/sqfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +0 -0
index 5d9c52af80..6405db4ff3 100644
@@ -8,6 +8,7 @@
  */
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void

Quoted 6 lines
fs/squashfs/sqfs.c +1 -0
 #include <asm/unaligned.h>
+#include <div64.h>
 #include <errno.h>
 #include <fs.h>
 #include <linux/types.h>
@@ -1440,7 +1441,7 @@ int sqfs_read(const char *filename, void

*buf, loff_t offset, loff_t len,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void · line 1446

Quoted 7 lines
fs/squashfs/sqfs.c +1 -1
      }
 
      for (j = 0; j < datablk_count; j++) {
-             start = data_offset / ctxt.cur_dev->blksz;
+             start = do_div(data_offset, ctxt.cur_dev->blksz);
              table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
              table_offset = data_offset - (start *

ctxt.cur_dev->blksz);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void

Quoted 2 lines
fs/squashfs/sqfs.c +0 -0
              n_blks = DIV_ROUND_UP(table_size + table_offset,
@@ -1516,7 +1517,7 @@ int sqfs_read(const char *filename, void

*buf, loff_t offset, loff_t len,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void · line 1522

Quoted 7 lines
fs/squashfs/sqfs.c +1 -1
              goto out;
      }
 
-     start = frag_entry.start / ctxt.cur_dev->blksz;
+     start = do_div(frag_entry.start, ctxt.cur_dev->blksz);
      table_size = SQFS_BLOCK_SIZE(frag_entry.size);
      table_offset = frag_entry.start - (start *

ctxt.cur_dev->blksz);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

fs/squashfs/sqfs.c int sqfs_read(const char *filename, void · line 1523

Quoted 1 line
fs/squashfs/sqfs.c +0 -0
      n_blks = DIV_ROUND_UP(table_size + table_offset,

ctxt.cur_dev->blksz);

Quoted 30 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Adding maintainers...

I guess that's a correct fix, so

Quoted 7 lines
Quoted 4 lines

Thanks,
Miquèl

Hello! do_div() macro modifies its first argument. It is correct? Because previous code did not modify anything.

* The semantics of do_div() are:
*
* u32 do_div(u64 *n, u32 base)
* {

 *    u32 remainder = *n % base;
 *    *n = *n / base;
 *    return remainder;

* }

And this patch replaced:

  start = data_offset / ctxt.cur_dev->blksz;

by:

  start = do_div(data_offset, ctxt.cur_dev->blksz);
Quoted 8 lines

which expands to:

  start = data_offset % ctxt.cur_dev->blksz;
  data_offset = data_offset / ctxt.cur_dev->blksz;

Which is wrong as it calculates something totally different.

So this patch is incorrect.

Hi Pali,

Quoted 8 lines
Quoted 3 lines

Yes I agree, I got tricked by some wrong cache from Yocto :/ I have tried lldiv(), and that works on our x86 target.

start = data_offset;
do_div(start, ctxt.cur_dev->blksz);

Something like this?

This is another option and seems that should be OK too.

Quoted 9 lines

Michael

Quoted 6 lines

Do you have an opinion on that?
Or do you have another idea to avoid the
"undefined reference to `__udivmoddi4'" error?

/Sean